語境
我正在使用NextJS v11.1.1-canary.11,React v17.0.2和Typescript v4.3.5.
我想基于NextJS Typescript 檔案創建一個簡單的 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)
完整的堆疊跟蹤在這里
我已經做過研究,但在 Google 或 Stackoverflow 上找不到像我這樣的案例,我是 NextJS 新手,所以我可能誤解了一些東西。有人知道我做錯了什么嗎?
uj5u.com熱心網友回復:
你的例子作業正常。
您可以通過將login.ts檔案放在pages/api/{whatever_you_want}
下一個檔案:
檔案夾 pages/api 內的任何檔案都映射到 /api/* 并將被視為 API 端點而不是頁面。它們只是服務器端包,不會增加客戶端包的大小。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/402108.html
