How to set custom favicon in node.js/express

I recently started working in node.js and in the app.js file there is this line:

app.use(express.favicon());

Now, how do I set up my own custom favicon.ico?

Thanks!

In Express 4

Install the favicon middleware and then do:

var favicon = require('serve-favicon');

app.use(favicon(__dirname + '/public/images/favicon.ico'));

Or better, using the path module:

app.use(favicon(path.join(__dirname,'public','images','favicon.ico')));

(note that this solution will work in express 3 apps as well)

In Express 3

According to the API, .favicon accepts a location parameter:

app.use(express.favicon("public/images/favicon.ico")); 

Most of the time, you might want this (as vsync suggested):

app.use(express.favicon(__dirname + '/public/images/favicon.ico'));

Or better yet, use the path module (as Druska suggested):

app.use(express.favicon(path.join(__dirname, 'public','images','favicon.ico'))); 

app.use(express.favicon(__dirname + '/public/images/favicon.ico')); 

I had it working locally without the __dirname + but couldn't get it working on my deployed server.

If you are using Express > 4.0, you should go to serve-favicon

No extra middlewares required. Just use:

app.use('/favicon.ico', express.static('images/favicon.ico'));