bento
bento copied to clipboard
Enforce CORS middleware
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));
})
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()?