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

API Routes not working #12411

Closed
KwaminaWhyte opened this issue Nov 28, 2024 · 4 comments
Closed

API Routes not working #12411

KwaminaWhyte opened this issue Nov 28, 2024 · 4 comments
Labels

Comments

@KwaminaWhyte
Copy link

I'm using React Router as a...

framework

Reproduction

I did this in routes.ts

...prefix('api/v1', [
    route('register', './routes/apis/v1/register.ts'),
    route('login', './routes/apis/v1/login.ts'),
  ]),

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (4) x64 Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
    Memory: 3.51 GB / 15.92 GB
  Binaries:
    Node: 20.18.1 - C:\Program Files\nodejs\node.EXE     
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD        
    bun: 1.1.36 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (131.0.2903.63)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    @react-router/dev: ^7.0.1 => 7.0.1 
    @react-router/node: ^7.0.1 => 7.0.1 
    @react-router/serve: ^7.0.1 => 7.0.1 
    react-router: ^7.0.1 => 7.0.1 
    vite: ^5.4.11 => 5.4.11

Used Package Manager

npm

Expected Behavior

To return json response

Actual Behavior

The following error is a bug in React Router; please open an issue! https://github.com/remix-run/react-router/issues/new/choose
Error: Expected a Response to be returned from resource route handler
    at invariant3 (C:\projects\_pewvegas\pm-travels-and-tours-website\node_modules\react-router\dist\production\index.js:8684:11)   
    at handleResourceRequest (C:\projects\_pewvegas\pm-travels-and-tours-website\node_modules\react-router\dist\production\index.js:9420:5)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at requestHandler (C:\projects\_pewvegas\pm-travels-and-tours-website\node_modules\react-router\dist\production\index.js:9206:18)
    at nodeHandler (C:\projects\_pewvegas\pm-travels-and-tours-website\node_modules\@react-router\dev\dist\vite.js:1967:30)
    at C:\projects\_pewvegas\pm-travels-and-tours-website\node_modules\@react-router\dev\dist\vite.js:1973:17

@mackehansson
Copy link

Are you returning the correct response in register.ts and login.ts?

@KwaminaWhyte
Copy link
Author

Yes

{
username: "username",
email: "email"
}

@ASgmbv
Copy link

ASgmbv commented Nov 28, 2024

You need to return Response object

@sergiodxa
Copy link
Member

Try to return Response.json({ username: "username", email: "email" })

While in UI routes you can return naked objects, in API routes you need to return a response, I recommend you to always return responses for consistency.

@timdorr timdorr closed this as completed Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants