简体   繁体   中英

hapi.js cors with auth

Put together the below.

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

I want to use with client-side JavaScript browser fetch API. The cors part is necessary to avoid using the no-cors mode for fetch and to get a non-opaque response. If I use only 'auth in the config section or only cors` they work fine, but together hapi complaints that the configuration is wrong.

Why is that?

inside config object you cannot use key cors . for correct configuration you have to put cors key inside this like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM