Here
I like to put my CORS handler as a piece of middleware in my Express server. I assume yours is app.js because there is no other server file you mentioned.
var allowCrossDomain = function(req, res, next) {
if ('OPTIONS' == req.method) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
you need to define this in your server.js file first like this, you need to tell the server to expect cross domain requests.
Please read the following articles if you still don't understand how CORS works or how to configure it.
http://jonathanmh.com/how-to-enable-cors-in-express-js-node-js/
http://justindavis.co/2015/08/31/CORS-in-Express/
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
res.header("Access-Control-Allow-Methods", "GET, POST","PUT");
next();
});
OR you could also sent it from your router.post for instace
route.post('/abc', function(req,res,next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
return res.send(200);
});
and then send the headers from angular part.