Node: CORS Fails Occasionally

3.6k views Asked by At

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();
});
2

There are 2 answers

1
Błażej Grzeliński On BEST ANSWER

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

0
John Doe On

I've been struggling with this for a while. I would get CORS errors...occasionally, when performing the same requests, to the same API.

The network tab in the devtools mentions about missing CORS origin, or other CORS related error, so this can misguide you into reviewing your CORS settings.

Detailed server logs however, indicate it's not a CORS issue. The problem comes from errors returned by the server following the request.

In my case, the error sent by the server generated CORS related issues, but these CORS issues were not the core issues here.

To summarize, have access to detailed server logs, and review how you process errors in your server response (such errors as an undefined value you're reading from the request body, etc...)

If I understood correctly, @Błażej Grzeliński above, experienced the same problems due to bad errors management in the server response.