Reputation: 5677
Why is my NodeJS app firing two requests
every time? I am not sure why this OPTIONS is getting fired each and every time, when i am actually only calling the HTTP Methods => [GET, POST, PUT, DELETE]
OPTIONS /api/v1/admin/user/55e1d606803478cc1edacfa0 200 0.149 ms - -
DELETE /api/v1/admin/user/55e1d606803478cc1edacfa0 200 7.598 ms - 58
Below is my code where i am calling OPTIONS.
app.all('/*', function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Access-Token,X-Key");
if(req.method === 'OPTIONS'){
res.status(200).end();
} else {
next();
}
});
Because of this two users
are getting deleted at the same time.
Upvotes: 0
Views: 158
Reputation: 1074148
Why is my NodeJS app firing two requests every time?
It isn't; the browser is.
I am not sure why this OPTIONS is getting fired each and every time, when i am actually only calling the HTTP Methods =>
[GET, POST, PUT, DELETE]
Most likely because you're making cross-origin calls, using ajax in a web browser. The OPTIONS
call is the preflight call the browser makes prior to the real call to ensure that the destination server accepts cross-origin calls from a given origin and, if so, to find out what headers, methods, credentials, etc. the server accepts from that origin.
More in the specification.
Not all cross-origin calls require a preflight, but many (possibly most) do; the rules are here.
Upvotes: 4