节点Express"favicon.ico"找不到错误 [英] Node Express "favicon.ico" not found error

查看:61
本文介绍了节点Express"favicon.ico"找不到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用Express作为查询字符串传递的URL下载照片,但是每次尝试使用它时,都会出现错误:无效的URI"favicon.ico" 有没有办法让我的浏览器停止请求收藏夹图标?对于下载图像,我使用的是 image-downloader 软件包( NPM页面)

I'm trying to download a photo through a URL passed as a query string using Express, but every time I try to use it, I get Error: Invalid URI "favicon.ico" Is there a way I can get my browser to stop requesting a favicon? For downloading images, I'm using the image-downloader package (NPM page)

代码:

app.get('/:url', (req, res) => {
let url = req.params.url;
const options = {
    url: url,
    dest: /path'
};
download.image(options)
    .then(({ filename, image }) => {
        console.log('File saved to ', filename);
})
    .catch((err) => {
        console.log(err);
    });
res.send("Done");

});

推荐答案

仅在服务器中为favicon.ico路由可能是最简单的.

It's probably easiest to just make a route for favicon.ico in your server.

app.get('/favico.ico', (req, res) => {
    res.sendStatus(404);
});

当然,如果需要,您实际上也可以发送一个有效的图标,但这至少将使Express服务器不会显示错误.

Of course, you could actually send a valid icon too if you wanted, but this will at least keep your Express server from showing an error.

仅供参考,这与 image-downloader 无关.这与浏览器请求一个favico.ico图标有关,该图标用于显示在URL栏中(以及浏览器UI中的其他位置).如果您的服务器为favicon.ico返回404,则浏览器将在其UI中使用通用图标.

FYI, this has nothing to do with the image-downloader. This has to do with the browser requesting a favico.ico icon that it uses to show in the URL bar (and some other places in the browser UI). If your server returns a 404 for favicon.ico, the browser will use a generic icon in its UI.

如果您想使自己成为一个简单的favico.ico,可以在此处进行操作,它将帮助您生成一个,然后您可以将上述路线更改为:

If you want to make yourself a simple favico.ico, you can go here and it will help you generate one and then you can change the above route to:

app.get('/favico.ico', (req, res) => {
    res.sendFile("myfavico.ico");
});

这篇关于节点Express"favicon.ico"找不到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆