ali forghani
ali forghani

Reputation: 11

why i receive cors error when i use subdomain in node js

my back end is node js, i set cors setting like that :

const whitelist = ['https://backend.example.com', 'https://example.com']
            const corsOptions = {
            credentials: true,
            origin: function(origin, callback) {
                console.log('Received origin:', origin, 'callback:', callback);
                if (whitelist.indexOf(origin) !== -1) {
                callback(null, true)
                } else {
                callback(new Error('CORS ERROR'))
                }
            }
            }
            this.#app.use(cors(corsOptions))

my back end run in subdomain: backend.example.com

When I send request from example.com (with axios in front end(nextjs)) i can receive response. but when send request from subdomain(with swagger) I received cors error. How can I solve it?

I change code several time but I can't fix the error.

Upvotes: 1

Views: 204

Answers (0)

Related Questions