server.js 724 B

1234567891011121314151617181920212223242526272829
  1. const http = require('http');
  2. let connectionMetadata;
  3. http.createServer(function(req, res) {
  4. console.log(req.method.toUpperCase(), req.url);
  5. if (req.method === 'POST') {
  6. let body = '';
  7. req.on('data', chunk => body += chunk);
  8. req.on('end', () => {
  9. connectionMetadata = body;
  10. res.writeHead(200);
  11. res.end();
  12. });
  13. return;
  14. }
  15. if (req.method === 'GET') {
  16. res.writeHead(200, {'Content-Type': 'text/plain'});
  17. res.end(connectionMetadata);
  18. return;
  19. }
  20. if (req.method === 'OPTIONS') {
  21. res.writeHead(200, {
  22. 'Access-Control-Allow-Origin': '*',
  23. });
  24. res.end(connectionMetadata);
  25. return;
  26. }
  27. console.error('unknown method: ' + req.method);
  28. }).listen(8000);