0

I am learning Node.js and I have created this simple http server.

var http = require('http'); var server = http.createServer(function(req,res){ console.log('request accepted'); res.end(); }); server.listen(3000,function(){ console.log("Server listening on port #3000"); }); 

It is working fine but when I visit http://localhost:3000/ the console logs request accepted thing twice. In short, if I'm understanding correctly, the request is received twice.

Is it a correct behavior or am I doing something wrong here?

enter image description here

1

2 Answers 2

6

Your browser makes two HTTP requests. One for the page at / and the other for /favicon.ico.

You can prove this by inspecting req.url.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Brad :) Is there any way I can ignore the request for favicon.ico?
Check req.url. If it's /favicon.ico then end the request immediately with a 404.
0

also check:

req.method 

if it's a POST call may be preceeded by a preflight (OPTIONS method)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.