I am just beginning to play around with node.js and was looking through the documentation. This code doesn’t even run:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(80, "127.0.0.1");
http.Server.addListener('request', function(req,res){
console.log(req.headers);
});
console.log('Server running at http://127.0.0.1');
I am trying to add a listener to the server object for the ‘request’ event. Under the documentation ‘request’ is listed as an event under http.Server.
Am I fundamentally misunderstanding something here? How would you go about adding a seperate listener function for the ‘request’ event? (that is, not overwriting the one added during createServer).
It looks like
listenisn’t chainable, and you’re not storing your server object. Try:That seems to work in my testing.