now.json `routes.status` not working as expected?
See original GitHub issueI have this simple now.json
file :
{
"version": 2,
"builds": [
{ "src": "foo.ts",
"use": "@now/node" }
],
"routes": [
{ "src": "/.*",
"methods": ["OPTIONS"],
"headers": {
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Origin": "*"
},
"status": 200,
"continue": false }
]
}
and foo.ts
:
export default async function(req, res) {
return res.send("You shouldn't see this when request's method is OPTIONS");
}
My problem is that when I send an OPTIONS
request to /foo.ts
the CORS headers are correctly set on the response but the route handler in foo.ts
is called too, while I expect now to stop before and automatically return a 200
.
The idea behind this is to avoid having to manually answer OPTIONS
requests with a 200
in every route my API exposes because that’s very redundant, so I’d like Now to do it automatically.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
now.json `routes.status` not working as expected? · Issue #3015 ...
My problem is that when I send an OPTIONS request to /foo.ts the CORS headers are correctly set on the response but the...
Read more >Python Flask App route not working as expected
Are you restarting the server when reloading @app.route('/') ? Also, try clearing your cache. With Chrome, you can do that by hitting SHIFT ......
Read more >Writing API Tests with Jest - Rithm School
Now there's actually a problem here when we want to start testing! In our test file we are going to include the app...
Read more >Using Express.js Routes for Promise-based Error Handling
Find out how to enable promise-based route code and centralize both error handling and normal-results handling in Express.js apps.
Read more >How to Test Your Express API with SuperTest - Rahman Fadhil
In this test, we make a POST request to /api/posts endpoint and send our post data. Then, we expect the response status to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @styfle 😃 Thanks for your answer.
Actually I ended up doing exactly what you describe in your first message :
The first route adds CORS headers to every request made to the API, and the second route simply responds to all
OPTIONS
requests :I just misunderstood the
status
docs when it says Now will respond with this code when a matching path is requested. I thought it would also automatically end the response. Maybe this should be clarified, andcontinue: false
’s behavior too.@flawyte Thank you so much, that solved my issue perfectly! The docs were very unclear about setting the cors headers in the
now.json
. I was doingres.setHeader
in the actual function.