最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NextJS calling api route from another api route - Stack Overflow

programmeradmin3浏览0评论

Is there a way to use fetch api from inside api route in NextJs? I have quite a big handler and instead of doing everything in one handler I would like to make it more modular so that after doing a job that is made for (file name el. write-to-db) I would like to call another handler eg (send-emails). Is it possible in NextJS?

Thanks!

Is there a way to use fetch api from inside api route in NextJs? I have quite a big handler and instead of doing everything in one handler I would like to make it more modular so that after doing a job that is made for (file name el. write-to-db) I would like to call another handler eg (send-emails). Is it possible in NextJS?

Thanks!

Share Improve this question asked Jun 27, 2022 at 14:30 sevenseven 1,7043 gold badges23 silver badges53 bronze badges 3
  • 1 Why don't you just put everything into different functions (and/or files) and call them sequentially? You shouldn't be calling internal API routes for other API routes. – juliomalves Commented Jun 28, 2022 at 18:29
  • 2 reasons are caching and consistency of code. Also that API call might live somewhere else one day, and ideally that would mean just changing the url – Adam Tolley Commented Mar 3, 2023 at 10:37
  • @juliomalves correct answer – Markus Kottländer Commented May 3, 2024 at 4:55
Add a ment  | 

1 Answer 1

Reset to default 10
const baseUrl = process.env.VERCEL_URL ? 'https://' + process.env.VERCEL_URL : 'http://localhost:3000'

await fetch(`${baseUrl}/api/[your-route-name-here]`)
发布评论

评论列表(0)

  1. 暂无评论