bento icon indicating copy to clipboard operation
bento copied to clipboard

Enforce CORS middleware

Open daghan opened this issue 5 years ago • 1 comments

Is your feature request related to a problem? Please describe. When using express routes, for every get/post/put/delete, I have to manually add cors options (using https://www.npmjs.com/package/cors).

I have to update more than 30 API end-points and I am not sure if I forgot anything.

Describe the solution you'd like I wish Bento warned me if I forgot to add cors options to any of the API calls.

https://github.com/daghan/serverside_js/blob/master/conFusionServer/routes/dishRouter.js

dishRouter
  .route("/")
  .options(cors.corsWithOptions, (req, res) => {
    res.sendStatus(200);
  })
  .get(cors.cors, (req, res, next) => {
    Dishes.find({})
      .populate("comments.author")
      .then(
        dishes => {
          res.statusCode = 200;
          res.setHeader("Content-Type", "application/json");
          res.json(dishes);
        },
        err => next(err)
      )
      .catch(err => next(err));
  })

daghan avatar Oct 11 '19 15:10 daghan

What is the rule informally? foreach xxx.get() where xxx is of type CorsRoute(?) then make sure that there is also a .options in the chain of method calls before or after those get()/post()/put()/delete()?

aryx avatar Oct 18 '19 15:10 aryx