Ro Bautista
Ro Bautista

Reputation: 21

Unable to set header with Hapi.js reply

when i add a random header to a reply in Hapi.js, it works fine.

However when I am trying to add to a header that already exisits, it is not sent.

It is almost as if i didn't even add it.

 reply("Hello World)
      .header("Random-Header", "Random Reply")
      .header("Access-Control-Expose-Headers","Authorization");

Is there a way to add the Access-Control-Expose-Headers - Authorization?

Upvotes: 2

Views: 1014

Answers (1)

kaplona
kaplona

Reputation: 31

If you want to use CORS headers you should enable/configure them in route options (CORS headers disabled by default):

server.route({
  method: 'GET',
  path: '/',
  handler: function(request, reply) { ... },
  config: {
    cors: {
      origin: ['*'], // list of valid domains
      exposedHeaders: ['Authorization'] // 'Access-Control-Expose-Headers'
    }
  }
});

Read more about defaults and options in hapi documentation.

or you can enable CORS on all routes:

new Hapi.Server({
  connections: {
    routes: {
      cors: {
        origin: ['*'],
        exposedHeaders: ['Authorization']
      }
    }
  }
})

Upvotes: 3

Related Questions