Search code examples
node.jsrestexpresscors

CORS error in node express REST API (PATCH Request)


I'm getting cors error for patch request. I'm using cors npm package, It's working well for get and post request. for patch method throwing cors error.

const express = require("express");
const cors = require("cors");
const app = express();

app.use(cors());

I added options app.options('*', cors()), no luck not working.

Error: Access to XMLHttpRequest at 'http://example.com/url' from origin 'http://localhost:8100' has been blocked by CORS policy: Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.


Solution

  • you are able to pass option to cors middleware , I hope this will help

    var options = {
      "origin": "*",
      "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
      "preflightContinue": false,
      "optionsSuccessStatus": 204
    }
    app.use(cors(options));