rad*_*her 4 typescript server nuxt.js nuxtjs3
我有以下 API URL 之一。归根结底,对于我的用例来说,我必须使用这些 URL 中的哪一个并不重要,但目前两者都不起作用。
http://localhost:3000/api/track/TRACKID
Run Code Online (Sandbox Code Playgroud)
或者
http://localhost:3000/api/track?id=TRACKID
Run Code Online (Sandbox Code Playgroud)
我如何获取TRACKID
API 代码?以下代码的结果是undefined
.
export default async (req: IncomingMessage, res: ServerResponse) => {
console.log(req.id);
};
Run Code Online (Sandbox Code Playgroud)
我尝试按照以下方式设置第一个 URL 的文件,但也没有成功。它只是导致 URL 变成http://localhost:3000/api/track/[id]
PROJECT/server/api/track/[id].ts
Run Code Online (Sandbox Code Playgroud)
对于第二个 URL,我使用了以下设置。
PROJECT/server/api/track.ts
Run Code Online (Sandbox Code Playgroud)
编辑:现在 Nuxt 3 已经发布了,更新了我的答案。
export default defineEventListener(event => {
const query = getQuery(event)
})
Run Code Online (Sandbox Code Playgroud)
https://github.com/unjs/h3是将 api 放在一起的服务器。
归档时间: |
|
查看次数: |
6316 次 |
最近记录: |