How to set custom favicon in Express?

前端 未结 14 1903
星月不相逢
星月不相逢 2020-12-12 12:49

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 cu

14条回答
  •  爱一瞬间的悲伤
    2020-12-12 13:12

    If you want a solution that does not involve external files, and does not use express.static (for example, a super-light one file webserver and site) you can use serve-favicon and encode your favicon.ico file as Base64. Like this:

    const favicon = require('serve-favicon');
    const imgBuffer = new Buffer.from('IMAGE_AS_BASE64_STRING_GOES_HERE', 'base64');
    // assuming app is already defined
    app.use(favicon(imgBuffer));
    

    Replace IMAGE_AS_BASE64_STRING_GOES_HERE with the result of encoding your favicon file as Base64. There are lots of sites that can do that online, have a search.

    Note you may need to restart the server and/or browser to see it working on localhost, and a hard refresh/clear browser cache to see it working on the web.

提交回复
热议问题