Express js prevent GET /favicon.ico

前端 未结 4 1571
忘掉有多难
忘掉有多难 2020-12-07 23:13

In every request, my server is receiving GET request to /favicon.ico, even when it\'s REST api that not include html file. Why is this happening and how can I prevent this r

4条回答
  •  盖世英雄少女心
    2020-12-07 23:50

    I agree with @Blair Anderson that middleware is the best course of action here but 204 should not return a body. Also, you may want to catch all favicon request e.g.: https://example.com/some/path/favicon.ico. In which case something like this works best:

    app.use( function(req, res, next) {
    
      if (req.originalUrl && req.originalUrl.split("/").pop() === 'favicon.ico') {
        return res.sendStatus(204);
      }
    
      return next();
    
    });
    

提交回复
热议问题