-
-
Save durlabhjain/30c7ded762a14a1c5667089b3b19b628 to your computer and use it in GitHub Desktop.
HTTP listener
This file contains 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
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