如何为 Content-Type: application/json 添加网站图标 (favicon)

huangapple go评论74阅读模式
英文:

How to add favicon for Content-Type: application/json

问题

我正在使用Node.js实现一个返回Content-Type: application/json响应的API,像这样:

module.exports={
    api: (req, res) => {
        let data = {"data": [1, 2, 3]};
        res.status(200).json(data);
    }
}

但是,在浏览器上尝试该API时,无法查看到图标。我看到另一个网站可以完成这个任务。

如何为 Content-Type: application/json 添加网站图标 (favicon)

如何在具有Content-Type: application/json的Node.js API上添加一个图标?

英文:

I am using node.js and implementing an API that returns a response as Content-Type: application/json like this:

module.exports={
    api: (req, res) => {
        let data = {"data": [1, 2, 3]};
        res.status(200).json(data);
    }
}

But, there is no favicon that can be viewed when trying that API on the browser. I see another website that can get this done.

如何为 Content-Type: application/json 添加网站图标 (favicon)

How can add a favicon on the Node.js API with Content-Type: application/json?

答案1

得分: 4

我无法理解你的请求。请提供需要翻译的具体内容。

英文:

Can't believe people are suggesting some package for this simple operation.

All browsers request /favicon.ico looking for favicon so you have to respond to this request.

app.get('/favicon.ico', (req, res) => {
  res.sendFile(path.join(__dirname, 'assets/favicon.ico'))
})

app.get('/api/foo', (req, res) => {
  res.json({ foo: 'bar' })
})

如何为 Content-Type: application/json 添加网站图标 (favicon)

Note: I used duckduckgo's favicon

答案2

得分: 2

你可以使用像express-favicon包提供的中间件,它会在API路由处理程序之前提供favicon.ico文件。

例如:

const favicon = require('express-favicon');
app.use(favicon(__dirname + '/public/favicon.png'));
英文:

You can use a middleware like the one provided from the express-favicon package that will serve the favicon.ico file before your API route handlers.

e.g

const favicon = require('express-favicon');
app.use(favicon(__dirname + '/public/favicon.png'));

答案3

得分: 1

你可以将你的favicon.ico添加到API的根目录中,也许浏览器会识别它。否则,对于API(本来就不应该在浏览器中查看的),没有设置favicon的方式...

英文:

You can just add your favicon.ico to the root folder of your API and maybe the browser will pick it up. Otherwise there is no way to have a favicon for API's (which are not to be viewed in the browser in the first place, so nobody cares about favicons for APIs...)

答案4

得分: 1

以下是翻译好的内容:

"favicon"的请求是浏览器在加载来自您的网站的页面时单独发出的请求(也可以来自浏览器之外)。它与您的服务器正在提供的其他请求无关(在您的情况下,API端点提供application/json响应)。

最简单的方法是像处理其他请求一样在您的应用程序中处理该请求:

app.get('/favicon.ico', (req, res) => {
    res.sendFile(
        // Path to the favicon file
    );
})

但是,您需要设置缓存行为,以防需要缓存。

您还可以使用express的serve-favicon包,它使设置缓存和处理其他事情变得更加容易(例如设置正确的Content-Type等)。

英文:

The request for favicon is a separate request that the browser makes whenever you load a page from your website (can be made from outside the browser too). It is independent of the other requests that your server is serving (in your case, the API endpoint serving application/json response).

Simplest way to do so would be to handle the request, like any other, in your application:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

app.get(&#39;/favicon.ico&#39;, (req, res) =&gt; {
      res.sendFile(
         //Path to the favicon file
      );
})

<!-- end snippet -->

However, you would need to set the caching behaviour, in case it is needed.

You can use express' serve-favicon package as well which makes it easy to set caching and handles other things as well (such as setting the correct Content-Type etc.).

答案5

得分: -3

首先,尝试将 .ico 文件放在公共文件夹中。

如何为 Content-Type: application/json 添加网站图标 (favicon)

其次,设置文件名。
例如,如果图标文件名是 file.ico,那么

&lt;link rel=&quot;shortcut icon&quot; href=&quot;%PUBLIC_URL%/file.ico&quot;/&gt;

英文:

First, try to put .ico file in public folder.

如何为 Content-Type: application/json 添加网站图标 (favicon)

Second, set the file name.
For example if favicon file name is file.ico, then

&lt;link rel=&quot;shortcut icon&quot; href=&quot;%PUBLIC_URL%/file.ico&quot;/&gt;

I hope this will help you.

huangapple
  • 本文由 发表于 2023年5月11日 20:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定