If I write the following program in node:
http.createServer(function (req, res) { if( req.method == 'GET' ) { var body = ''; req.on('data', function(data) { body += data }); req.on('end', function() { console.log('request ended') }); } res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('142\n'); }).listen(3500);
And then hit the server with http://xxx.xx.xxx.xx:35010
I see a request ended
twice on my console -- I'm not sure why a single HTTP request is causing this to execute twice.
createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.
Like most Node. js functions, createServer() takes a callback function as an argument. This callback function is executed each time the server receives a new request.
However, considering that a “Hello World” Node. js server is easily capable of thirty thousand requests per second on that machine that produced these results, 23 requests per second with an average latency exceeding 3 seconds is dismal.
createServer() passes two arguments to the callback when you call it. The first is the http request object, the second is the http response object. If your callback wants to work properly and use those arguments, it must create two arguments that match those. You can name them anything you want.
That is normal - your browser makes more than one call.
Most browsers make a call to grab /favicon.ico
for example.
Try to log the url:
console.log(req.url);
and you'll see what's being called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With