Next.js Discord

Discord Forum

deploy custom api route

Answered
LaPerm posted this in #help-forum
Open in Discord
LaPermOP
How do I deploy my custom api route to production using absolute url. I've tried using my vercel app's domain name to try accessing it like this . my-project-name.vercel.app/api/posts. But I get an error in production saying Unexpected token '<', "<!doctype "... is not valid JSON.
Answered by LaPerm
But why doesn't next come with CORS enabled automatically
View full answer

8 Replies

@LaPerm How do I deploy my custom api route to production using absolute url. I've tried using my vercel app's domain name to try accessing it like this . my-project-name.vercel.app/api/posts. But I get an error in production saying Unexpected token '<', "<!doctype "... is not valid JSON.
Sun bear
In general its correct.

Did you try to open it in the browser or fetch it?

It looks like you are trying to fetch it but its returning html instead of json what you are expecting. Did you chose the right method? (POST, GET)
LaPermOP
Yep. I used the right method. It turns out my deployment settings were interfering, and I had to enable CORS.
Sun bear
Yes if you are fetching from a different domain you have to allow it via next.config. So its working now?
LaPermOP
Yes thank you for your help
LaPermOP
But why doesn't next come with CORS enabled automatically
Answer
@LaPerm But why doesn't next come with CORS enabled automatically
Sun bear
I think its a build in security feature to keep the routes at least a bit secure by default
LaPermOP
Hmmm. Would probably explore more on that
Thanks again