问题
I have a node app with the server hosted on heroku. All my requests are successful until I have sent about 10 or 15. Then I start receiving CORS errors. Any idea why this could be occurring?
Give it a try. http://danielrasmuson.github.io/
Here is my 'CORS Enabling Code'. I'm trying a few things at this point.
var app = express();
app.use(cors());
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
回答1:
I do know not if it is not too late but maybe this can help: I had the same problem with this code: (occasionally i got cors erros with 503 heroku error)
router.post('/create', function (req, res, next) {
password(req.body.user_password).hash(function (error, hash) {
if (error)
throw new Error('Something went wrong!' + error)
console.log(req.body);
req.body.user_password = hash;
User.create(req.body, function (err, post) {
if (err) return next(err);
console.log(post);
res.json(post);
});
});
});
When I changed it to :
router.post('/create', function (req, res, next) {
password(req.body.user_password).hash(function (error, hash) {
if (error) {
throw new Error('Something went wrong!' + error);
} else {
req.body.user_password = hash;
User.create(req.body, function (err, post) {
if (err) {
throw new Error('Something went wrong!' + err);
} else {
res.json(post);
}
});
}
});
});
No more failing code and cors occasionally.
Finally it seems to be problem of node app and not heroku.
Cheers Blazej
来源:https://stackoverflow.com/questions/27157195/node-cors-fails-occasionally