Skip to content

Instantly share code, notes, and snippets.

@gauravchl
Last active January 14, 2018 12:41
Show Gist options
  • Save gauravchl/39900b3894ad398afc0a0114cd97750b to your computer and use it in GitHub Desktop.
Save gauravchl/39900b3894ad398afc0a0114cd97750b to your computer and use it in GitHub Desktop.
Simple node server
#!/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