Jackie
Jackie

Reputation: 23527

CORS not working in Express after config

I have two express-based servers...

Server 1: API runs on port 3010

Server 2: UI runs on port 3000

On server 1 in the app.js file (autogen) I have the following...

// Allow requests from the ui
app.use(function (req, res, next) {
    // TODO: Make specific
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    next();
});

But when I try to POST to that address from a Server 2 page I get the following in the Chrome Console....

Failed to load http://localhost:3010/search: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

I also tried res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); and others but none seem to work

What am I missing?

Upvotes: 1

Views: 378

Answers (1)

Jackie
Jackie

Reputation: 23527

I moved the app.use ahead of the other generated app.uses and it seems to work. Still not sure why, guessing a middleware item in the autogen isn't calling next properly.

Upvotes: 1

Related Questions