Using the OPTIONS method
Preface
Due to the built-in CORS middleware, the usage of the OPTIONS
method in routes
requires some additional configuration.
Mocks Server adds by default a middleware to enable CORS automatically in all routes. It also enables CORS pre-flight responses, so it will respond with a 204 status to all requests using the OPTIONS
method to any route. This means that the OPTIONS
method can't be used in routes
until this middleware is disabled, because the built-in CORS pre-flight middleware would send the request response before your route is executed.
Disabling CORS pre-flight response
If you want to handle all OPTIONS
requests by yourself, you can enable the preflightContinue
option of the cors
middleware using the configuration file:
module.exports = {
server: {
cors: {
options: {
preflightContinue: true
}
}
}
};
Read the configuration chapter for further info.
Enabling CORS pre-flight response only for some paths
Mocks Server uses the cors
npm package under the hood to enable CORS, so you could still enable it only for your desired routes using the same package even if you disabled it globally.
To do that, you should install cors
and create a route adding the cors
middleware to the correspondent path.
npm i --save-dev cors
const cors = require("cors");
module.exports = [
{
"id": "cors",
"url": "/api/users/*", // paths to enable cors
"method": ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"], // HTTP methodS
"variants": [
{
"id": "enabled",
"type": "middleware",
"options": {
"middleware": cors(),
}
},
]
}
];
Then, remember to add your route to the beginning of your "base" collection:
[
{
"id": "base", // collection from which the others extend
"routes": ["cors", "get-users:all", "get-user:id-1"] // add cors middleware to the beggining of the routes array
}
]
Read how to use middleware variants for further info.