I am making a cross-domain request (client-side and server-side). in server.js I added this code and the req cross domain is working fine
function setAcceptsHeader(req, res, next) {
'use strict';
res.setHeader('Access-Control-Allow-Origin', '*');
next();enter code here
}
app.options('*', function (req, res) {
'use strict';
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.status(200).end();
});
The problem is that I added req.redirect () to server.js, im getting an error
XMLHttpRequest cannot load http: // localhost: 1214 / # / about . The requested resource does not have an Access-Control-Allow-Origin header. Therefore, the initial value of "null" is not allowed.
app.get('/users/:email',setAcceptsHeader, function (req, res) {
User.findOne({email:req.params.email}, function (err, post) {
if(!post){
res.json({error:'item not found'});
}
else{
res.redirect('http://localhost:1214/#/about');
}
});
I know on the client side, I can use window.location for routing, but I want this to be implemented on the server side. Help!!
source
share