What is the best way to make requests to a external api with Next?

I have a nodejs server and would like to make requests from a next application. I read about Route handlers, but I don't know if it's the best strategy and how to implement it
7 Replies
rainy
rainy17mo ago
wouldn't fetch work?
Alefe Bispo - BRT
Alefe Bispo - BRTOP17mo ago
I think I'm a little confused. Normally, using react I would create an instance of axios and split the requests into files. With next can I do the same process? Considering requests made by the server
barry
barry17mo ago
Overthinking it, just fetch it
Keef
Keef17mo ago
2 much think +1 Its just fetches and maybe validation using zod if you want to be extra sure Its the same procedure for fetching in client side apps and server side. The difference being the server is the one making the request rather then the client. It doesn't really matter tho
const fetchTodos = async () => {
const data = await fetch(....).then(data=> data.json())

return await zodSchema.parseAsync(data)
}
const fetchTodos = async () => {
const data = await fetch(....).then(data=> data.json())

return await zodSchema.parseAsync(data)
}
Just invoke it where you need it
barry
barry17mo ago
What's the reason behind parseAsync and not just parse? So return can be caught instead of a try catch block?
Keef
Keef17mo ago
No real reason Atleast in my use case
Alefe Bispo - BRT
Alefe Bispo - BRTOP17mo ago
Thanks guys!
Want results from more Discord servers?
Add your server