Search code examples
corsmiddlewarerestify

Restify and vue cors cross domain blocked


I have a restify server running my API, and I am defining cors middleware as follow:

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser({
  multiples: true,
  mapParams: false
}));
server.pre(restify.CORS())
server.use(restify.fullResponse())
server.use(
  function crossOrigin(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
  return next();
});

but I am always getting this error message:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://myroute... (Reason: CORS preflight channel did not succeed).

What am I doing wrong?


Solution

  • It happened because restify.CORS() is deprecated at restify new version, at least I see it here:

    https://github.com/restify/node-restify/issues/1151

    "The restify CORS plugin will be deprecated in the upcoming 5.x release in favor of https://github.com/TabDigital/restify-cors-middleware"

    I changed for this plugin and everything worked again.