如何使用 HapiJs 为 404 设置 access-control-allow-origin?

How to set access-control-allow-origin even for 404 with HapiJs?

我将我的 HapiJS 服务器配置为:

const server = Hapi.server({
    host: env.HOSTNAME,
    port: env.PORT,
    routes :
        {
            cors: {
                origin: ['ignore'],
                headers: ['Accept', 'Authorization', 'Content-Type'],
            }
        }
});

路线示例: GET /api/v3/users { cors: { origin: 'ignore', headers: [ '*' ] }, validate: { query: { token: [Object] } } }

如果我直接从浏览器或邮递员请求它,我会得到以下 return :

所以我可以看到 access-control-allow-origin: * header 已设置。


但是,当我使用 XHR Requestreact 请求此资源时,我在 OPTIONS 请求中得到 404,然后是 CORS 错误,例如:

所以我得到一个 CORS 错误。

如何使用 HapiJs 为 404 设置 access-control-allow-origin

不幸的是,我没有直接通过 NodeJS 找到任何解决方案,因为我认为没有。

所以我使用了 Nginx reverse-proxy 服务器作为 Web 前门,总是提供正确的 Headers。