TungstenSteel
TungstenSteel

Reputation: 31

ExpressJS : Unexpected HTTP OPTIONS request status code 204

I've encountered a very stranger problems with ExpressJS. While I request my API with Insomnia (analog of Postman), it works fine and without any header except 'Content-Type' : 'application/json', it works fine.

Now I try to fetch data from browser using fetch:

const reponseCustomer = await fetch(url, {
                method : "POST",
                headers: {
                    'Accept' : 'application/json',
                    'Content-Type' : 'application/json',
                    'mode' : 'cors'
                },
                body : obj
            })

ExpressJS route signature:

router.post('/', async function(req, res, next)

And requests hearders:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
  next();
})

So after fetching, in terminal I get following log:

[9:41 PM] 
    OPTIONS /api/auth/register 204 1.629 ms - 0
POST /api/auth/register 400 8.824 ms - -

My question is why there is an made an request on OPTIONS route if I have none of them in entire project.

Any help is appreciated!

Upvotes: 0

Views: 1047

Answers (1)

Vicente Cisternas A
Vicente Cisternas A

Reputation: 1

If you proove putting your code app.use in the start of all middlewares of your app.

Upvotes: 0

Related Questions