How to enable DELETE request? How to enable DELETE request? express express

How to enable DELETE request?


I solved it without adding new packages, just added this line

res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");

Notice that i have my allowed methods separated by commas inside one string.The complete function looks like this:

app.use(function (req, res, next) {  res.header("Access-Control-Allow-Origin", "*");  res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");  next();});


One of your headers is not properly set, so instead of

res.header("Access-Control-Allow-Methods", "GET", "PUT", "POST", "DELETE", "OPTIONS");

put it as

res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");

But you're right, using the npm package is simpler.


Solved it by simply using npm's cors package and enabling all cors requests, by simply replacing...

// enable CORSapp.use(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", "Origin, X-Requested-With, Content-Type, Accept, Authorization");    next();});

with...

app.use(require('cors')());

But I'm still not sure what magic cors package is doing under the hood to make it work.