Next.js应用程序路由中的API路由迁移路由未找到。

huangapple go评论54阅读模式
英文:

Next Js Api routes in App router migration routes not found

问题

我正在尝试使用新的App路由功能调用Next.js API路由。我总是收到“未找到”错误。我按照文档中的结构进行了设置。

这是我的chat.js文件,从未被调用过?我是否有错误的结构?

export default async function handler(req, res) {
    const { message } = req.body;

    try {
        const response = await axios.post(
            // 在此处添加请求的URL
        );
        res.status(200).json(reply);
    } catch (error) {
        console.error(error);
        res.status(500).json({ error: "在处理请求时发生错误" });
    }
}
英文:

Next.js应用程序路由中的API路由迁移路由未找到。

Im trying to call nextJs api route with the new App route feature. I always get not found Back. I made the structure like in the docu.

This is my chat.js file which never gets called? Do ive the wrong structure?

  1. List item

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

export default async function handler(req, res) {
    const {message} = req.body;

    try {
        const response = await axios.post(
            
        res.status(200).json(reply);
    } catch (error) {
        console.error(error);
        res.status(500).json({error: &quot;An error occurred while processing the request&quot;});
    }

<!-- end snippet -->

答案1

得分: 0

就像page.js一样,你的API路由必须被称为route.js。

所以在你的例子中,它会像这样:
api(文件夹)=> chat(文件夹)=> route.js(文件)

你可以在这里阅读更多关于此的文档链接

英文:

Like page.js, your api route has to be called route.js.

So in your example it would be like this:
api(folder)=>chat(folder)=>route.js(file)

You can read more about this in docs here.

huangapple
  • 本文由 发表于 2023年6月15日 20:17:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定