英文:
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: "在处理请求时发生错误" });
}
}
英文:
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?
- 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: "An error occurred while processing the request"});
}
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论