Unable to Change Favicon with Express.js


This is a really basic question, but I'm trying to change the favicon of my node.js/Express app with

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

and I'm still getting the default favicon. This is in my app.configure function, and yes, I've verified that there is a favicon.ico in the /public/images/favicon.ico.There's nothing about a favicon.ico in the console, either, which leads me to believe that this line of code is being ignored. Everything else in the function (setting port, setting views directory, setting template engine. etc.) seems to be working fine, so why would this line of code not be executing?

What I tried

  • Emptying browser cache
  • Restarting Terminal and running node app.js again
  • Adding { maxAge: 2592000000 }, as described here

Thanks in advance.

Update: I got it to work. See my answer below for more information.

7/25/2012 9:48:57 PM

Accepted Answer

I tried visiting the site in Safari for the first time (I normally use Chrome) and noticed that it was showing the correct favicon. I tried clearing my cache in Chrome again (twice) to no avail, but after more searching, I found that apparently favicons aren't stored in the cache. I "refreshed my favicon" using the method described here and it worked!

Here's the method (modified from the above link), in case the link goes dead:

  1. Open Chrome/the problematic browser
  2. Navigate to the favicon.ico file directly, e.g. http://localhost:3000/favicon.ico
  3. Refresh the favicon.ico URL by pressing F5 or the appropriate browser Refresh (Reload) button
  4. Close the browser and open your website - voila, your favicon has been updated!
1/31/2017 12:37:00 AM

What worked for me finally:

Look that the

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

is at the beginning of the app configuration function. I had it before at the end. As the Express doc says: 'The order of which middleware are "defined" using app.use() is very important, they are invoked sequentially, thus this defines middleware precedence.'

I didn't need to set any maxAge.

To test it:

  • Restart the server with node app.js
  • Clear the browser cache
  • Refresh the Favicon with directly accessing it by using "localhost:3000/your_path_to_the favicon/favicon.ico" and reloading

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow