Reputation: 5590
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.
Upvotes: 88
Views: 20730
Reputation: 145950
Another thing to watch out for is that modern browsers may prefetch or preload pages. Chrome will sometimes even do this before you hit enter in the address bar!
It can be disabled under Privacy and security, but this will affect all browsing.
You can also check for header Purpose: prefetch
and just return an error. (I'm not sure what the official response should be in production.)
This is unlikely to be happening frequently, but if you're testing an API it can be at best annoying and at worse dangerous for an unexpected request to suddenly be made.
Upvotes: 0
Reputation: 25958
Generally, favicon.ico
is fetched by the browsers. So, the two calls.
Solution to this issue can be checking for request URL if it's fetching favicon.ico
or not.
http.createServer(function (req, res) {
if (req.url != '/favicon.ico') {
// do your stuffs
}
}).listen(3500);
Upvotes: 21
Reputation: 6339
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.
Upvotes: 189