Last active
January 14, 2018 12:41
-
-
Save gauravchl/39900b3894ad398afc0a0114cd97750b to your computer and use it in GitHub Desktop.
Simple node 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
#!/usr/bin/env node | |
const http = require('http'); | |
const getBody = (req) => { | |
return new Promise(resolve => { | |
let body = ''; | |
req.on('data', chunk => { body += chunk }); | |
req.on('end', _ => resolve(body)); | |
}) | |
} | |
const handleRequest = async (req, res) => { | |
const body = await getBody(req); | |
console.log(body); | |
res.writeHead(200, { 'Content-type':'text/plan' }); | |
res.write('Hello Node JS Server Response'); | |
res.end(); | |
} | |
const server = http.createServer(handleRequest).listen(8082); | |
// let contentType = req.headers['content-type'] || ''; | |
// if (contentType.indexOf('application/json') > -1) body = JSON.parse(body); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment