Saturday 8 April 2017

NodeJS Express: CORS enabled and still not working

I'm having a huge problem with CORS.
I'm developing in AngularJS using NodeJS Express and I have another API running in another IP/PORT (both are in the same machine). I'm coding functions in Angular to consume the API.

When I try to get/post/delete/put I receive this message:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3000' is therefore not allowed access.

There's a lot of solutions here in Stackoverflow to configure the app.js. One configuration that I've tried and still not working:

app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});  

I have installed the npm cors module. I've edited my app.js to enable the cors module and still not working:

var express = require('express');
var cors = require('cors');  
app.use(cors());  

Any solutions? I have no idea how to fix it! Btw I'm using Chrome.



via Victor

No comments:

Post a Comment