Google AI suggested too (independently, I did this first then searched
and got:
---
1. Using Middleware
A middleware function can intercept OPTIONS requests and set the
appropriate CORS headers.
```js
app.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", "Content-Type, Authorization");
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
```
---
Tested locally with curl on GET, OPTIONS, and POST and correctly lets
OPTIONS return an empty CORS block, etc. And the site works too :)
---------
Co-authored-by: Mats Jun Larsen <mats@jun.codes>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>