Skip to content

Instantly share code, notes, and snippets.

@durlabhjain
Created August 22, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durlabhjain/30c7ded762a14a1c5667089b3b19b628 to your computer and use it in GitHub Desktop.
Save durlabhjain/30c7ded762a14a1c5667089b3b19b628 to your computer and use it in GitHub Desktop.
HTTP listener
var http = require('http');
let requestId = 0;
http.createServer(function (req, res) {
requestId++;
console.log(new Date());
console.log(requestId + ": " + req.method);
//if (req.method === 'POST') {
let body = '', length = 0;
req.on('data', chunk => {
length += chunk.length;
body += chunk.toString(); // convert Buffer to string
});
req.on('end', () => {
//console.log(body);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
console.log(requestId + ": end. Size:" + length);
});
//}
}).listen(8000); //the server object listens on port 8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment