API Routes Not Found. Can You Help?
I've created a very simplified example to explain the errors I'm receiving when trying to create an API route.
First, I created an API route in
This is odd because that route does exist. 2. Even though I'm getting a 404 error, I'm still getting a console.log() output. How is that possible if the route cannot be found? 3. If I was using Express,
That output is not the JSON I passed in the body. So what am I doing wrong? Any help would be greatly appreciated.
routes/api/create-payment-intent.ts
. This route just logs the request body to keep things simple.
I've also created a page "StripePage" that uses fetch to make and HTTP POST request to the /api/create-payment-intent
endpoint.
Here are the issues.
1. When I visit "StripePage" I get the following error:
POST http://localhost:3000/api/create-payment-intent 404 (Cannot find any path matching /api/create-payment-intent.)
.
This is odd because that route does exist. 2. Even though I'm getting a 404 error, I'm still getting a console.log() output. How is that possible if the route cannot be found? 3. If I was using Express,
request.body
would give me access to the items
I passed in the body. But when I console.log(request.body) I get the following:
ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
That output is not the JSON I passed in the body. So what am I doing wrong? Any help would be greatly appreciated.
5 Replies
1. You have to return something from your route, else the route "does not exist", as it has no content.
2. The API call arrives at the server and the function gets called. It is found on the server, but does not exist for the client as it has no content or different status code
3.
request.body
is a ReadableStream
, but since you are using JSON, you can simply call await request.json()
and you will get the data
I hope that this is helpful, if you have any questions, feel free to ask 🙂@apollo79 Thank you!
I've taken multiple courses on building API servers, and none mentioned that you HAVE to return something from a POST request.
I just tried this out in a Go REST API, two different Node frameworks and I get the same error. So, you live and you learn. I guess that's why they say building things is the best way to learn.
I do have one question.
How did you know that
request.body was a ReadableStream
and that you had to use request.json()
? I didn't see that anywhere in the "API Routes" section of the SolidStart docs.
Is there another set of docs I can reference for this info?
Thanks again!solid-start provides you a web request object, so you can just refer to mdn: https://developer.mozilla.org/en-US/docs/Web/API/Request
MDN Web Docs
Request - Web APIs | MDN
The Request interface of the Fetch API represents a resource request.
Awesome! Much appreciated. Have a great day.
Happy to help 🙂
You too!