NextJS API error : "TypeError: res.status is not a function"

NextJS API error : "TypeError: res.status is not a function"

上下文

我正在使用 NextJS v11.1.1-canary.11React v17.0.2Typescript v4.3.5

我想基于 NextJS Typescript documentation 创建一个简单的 API 端点,所以我在 pages/proxy 文件夹中创建了一个名为 login.tsx 的文件,其中包含非常简单 API 代码:

import type { NextApiRequest, NextApiResponse } from 'next'

export default function Login(req: NextApiRequest, res: NextApiResponse) {
  res.status(200).json({ name: 'John Doe' })
}

我的问题

当我尝试调用那个 API 端点时,出现错误:

TypeError: res.status is not a function
    at Login (C:\wamp64\www\project-front\.next\server\pages\proxy\login.js:19:7)

完整堆栈跟踪here

我已经做过研究,但在 Google 或 Whosebug 上找不到像我这样的案例,我是 NextJS 的新手,所以我可能误解了一些东西。有人知道我做错了什么吗?

您的示例运行良好。

您可以通过将 login.ts 文件放在 pages/api/{whatever_you_want}

来解决这个问题

下一个文档:

Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page. They are server-side only bundles and won't increase your client-side bundle size.