question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

now.json `routes.status` not working as expected?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
flawytecommented, Sep 20, 2019

Hi @styfle 😃 Thanks for your answer.

Actually I ended up doing exactly what you describe in your first message :

{
  "routes": [
    { "src": "/.*",
      "headers": {
        "Access-Control-Allow-Headers": "*",
        "Access-Control-Allow-Methods": "*",
        "Access-Control-Allow-Origin": "*"
      },
      "continue": true },
    { "src": "/.*",
      "methods": ["OPTIONS"],
      "dest": "/src/routes/cors-handler.ts" },

    "... my API endpoints"
  ]
}

The first route adds CORS headers to every request made to the API, and the second route simply responds to all OPTIONS requests :

// cors-handler.ts

export default async function(req, res) {
  // There's nothing to do here except to end the response since CORS headers
  // are automatically added for all routes using the `now.json` file.

  return res.status(204).send();
}

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, and continue: false’s behavior too.

1reaction
crockcommented, Jan 27, 2020

@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 doing res.setHeader in the actual function.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found