hapi.js cors with auth

hapi.js cors with auth

将以下内容放在一起。

server.route([
  {
    method: "POST",
    path: "/authorize",
    config: { 
      auth: false,
      cors: {
        origin: ['*']
      }
    },
    handler: (request, reply) => {
      ...
      reply.redirect(redirectUrl)
    }
  }
])

我想使用客户端 JavaScript 浏览器获取 API。 cors 部分是避免对 fetch 使用 no-cors 模式并获得非不透明响应所必需的。 如果我只使用 'authin the config section or onlycors` 它们工作正常,但一起 hapi 抱怨配置错误。

这是为什么?

config 对象中您不能使用键 cors。为了正确的配置,你必须像这样

cors 键放在里面
server.connection({
  port: dbConfig.port,
  routes: { cors: true } // set cross origin by hapi inbuilt property
   // tls: tls
})