Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nitro returns error differently for /api and /routes #2201

Open
enkot opened this issue Feb 29, 2024 · 3 comments
Open

Nitro returns error differently for /api and /routes #2201

enkot opened this issue Feb 29, 2024 · 3 comments

Comments

@enkot
Copy link

enkot commented Feb 29, 2024

Environment

Operating System: Linux
Node Version: v18.18.0
CLI Version: 3.10.1
Nitro Version: 2.9.1
Package Manager: [email protected]

Reproduction

https://stackblitz.com/edit/github-zyt6se?file=routes%2Ffoo.ts,api%2Ffoo.ts,public%2Findex.html

Describe the bug

Check the response for /api/foo and /foo endpoints. Each of them throws same 404 error in the handler, but:

  • /foo -> returns error as html page
  • /api/foo -> returns error as json object as expected

Additional context

No response

Logs

No response

@manniL
Copy link
Contributor

manniL commented Feb 29, 2024

Your repo link is just the starter project? 🤔

I'd argue that this makes sense as server routes rather return "markup" by default while an API returns JSON by default.

@enkot
Copy link
Author

enkot commented Feb 29, 2024

@manniL Fixed the link 🙈

Based on the docs /api is just a convenient way to create routes and automatically prefix them with /api - https://nitro.unjs.io/guide#api.

This PR partially fixes the problem by allowing to rename the route prefix #1763
But some projects may want to have multiple "api" endpoints (f.e. /api, /cms, /commerce etc), so they should have possibility to response with JSON when throwing errors.

@MickL
Copy link
Contributor

MickL commented Apr 5, 2024

@pi0 Is it intentional that /routes returns an html page and /api a json response? If yes, can this be unified so both act the same? And at the same time add a config option to nitro.config.ts to enable/disable html responses?

Personally I dont want the /api prefix but also I dont want any html responses in my rest api at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants