Skip to content

Instantly share code, notes, and snippets.

@codebubb
Last active September 9, 2019 10:36
Show Gist options
  • Select an option

  • Save codebubb/0c32460d67ee99dec8a455b59ef82c57 to your computer and use it in GitHub Desktop.

Select an option

Save codebubb/0c32460d67ee99dec8a455b59ef82c57 to your computer and use it in GitHub Desktop.
Echo Server
// Simple Node.js Echo Server that listens for data and sends a response with the data back to the client
// Note: Responds to any verb eg. GET/POST etc.
const createServer = require('http').createServer;
const server = createServer((req, res) => {
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
});
res.end(`{ "response": "${body ? body : 'No body sent' }" }`);
})
});
server.listen(3000, () => {
console.log('Server listening');
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment