Last active
September 9, 2019 10:36
-
-
Save codebubb/0c32460d67ee99dec8a455b59ef82c57 to your computer and use it in GitHub Desktop.
Echo Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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