0

I am using node.js to run the server. Always when I make a request to it actually there are occur two requests, one of which is an ordinary one, which was truly made and one is request/favicon.ico. So I tried to send favicon.ico back because I want it to appear in the top bar. But it just doesn`t appear there.

enter image description here

What am I doing wrong? Here is my code:

var http = require("http");

http.createServer(onRequest).listen(7777);
console.log("Server is running now.....");

function onRequest(request, response)
{
    console.log("A user made a request" + request.url);
    response.writeHead(200, {"Context-Type": "text/plain"});
    response.write("Here is some data");
    response.end();
}

And I put file favicon.ico into the same folder my server.js is.

enter image description here

This question:Set favicon in HTTP server? is not appropriate for me since the answer and code in answer, which were accepted, for it don`t work for me.

Community
  • 1
  • 1

3 Answers3

1

You can do this by adding this line in your html page in head tag.

<link rel="icon" type="image/png" href=favicon.ico>

1

This should work

var http = require("http");
var fs = require("fs");

http.createServer(onRequest).listen(7777);
console.log("Server is running now.....");

function onRequest(request, response)
{
    console.log("A user made a request" + request.url);
    if (request.url === '/favicon.ico') {
      var fileStream = fs.createReadStream("./favicon.ico");
      return fileStream.pipe(response);
    }
    response.writeHead(200, {"Context-Type": "text/plain"});
    response.write("Here is some data");
    response.end();
}

But as you can see you have to create a special case for each url you want to handle. I recommend using a framework like express which will make things easy for you.

Also with framework you can have favicon in static directory so you won't have to explicitly read from file system on each request for static files.

Manan Vaghasiya
  • 881
  • 1
  • 10
  • 25
0

If you use http module directly then you will have to inspect the request object on every request and serve the favicon file yourself for all requests requesting favicons.

In your example, you will have to test the request object in the onRequest() function and serve a favicon for some requests, and your original stuff for the rest of the requests.

If you use Express or some other framework with Connect-compatible middleware, then you'll be able to use modules for that like this one:

If you want to use just the http module without Express or any other higher level framework, see this answer for examples on how to serve static images with http (and also with Express) that will help you with that:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • Could you provide me with the code for my case, please? Such code, that it will send `favicon.ico` on ordinary request and I will be able to see the `favicon.ico` in the top bar. I am using the server without any frameworks. –  Mar 14 '17 at 12:20