Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Hapi - How to enable cross origin access control

I am working HapiJs Restful web service and trying to enable cors so any client even from different domain can consume my services. I tried cors=true in server connection object but didn't work.

like image 373
Gunjan Kumar Avatar asked Jun 19 '16 06:06

Gunjan Kumar


2 Answers

Where did you put cors=true? Could you add some code?

Without know exactly where you've put cors = true, this bit of code may help you:

server.connection({ routes: { cors: true } })

Or try adding the allowed cors in the config section of your route.

server.route({
    config: {
        cors: {
            origin: ['*'],
            additionalHeaders: ['cache-control', 'x-requested-with']
        }
    },

Take a look at this question: hapi.js Cors Pre-flight not returning Access-Control-Allow-Origin header

like image 194
James111 Avatar answered Sep 29 '22 03:09

James111


Addition to @James111's answer,

Even if that answer doesn't work for you. check for additional Auth headers that you are sending.

In my case it was X_AUTH_TOKEN, so in additionalHeaders you might want to add your custom header as well.

e.g.

additionalHeaders: ['cache-control', 'x-requested-with', 'X_AUTH_TOKEN']
like image 28
shyammakwana.me Avatar answered Sep 29 '22 04:09

shyammakwana.me