我的API路线的一部分:
const handler = async (req, res) => {
if (req.method === "POST") {
...
} else if (req.method === "DELETE") {
...
} else {
res.status(405).json({
data: null,
error: "Method not allowed."
});
return;
};
};
export const config = {
api: {
bodyParser: false,
},
};
export default handler;
我考虑过将它们分成两个不同的文件,如下所示:电流设置:
/api/advard/[advartid]/image/index.jsx
/api/advard/[advartid]/image/post.jsx
/api/advard/[advartid]/image/delete.jsx我只是遇到了这个问题,并使用了
stripe的github
的功能来做到这一点。 const body = await buffer(req);
const bodyAsString = body.toString("utf8");
const jsonOfRequest = JSON.parse(bodyAsString);
console.log(jsonOfRequest);
wherebuffer
const buffer = (req: NextApiRequest) => {
return new Promise<Buffer>((resolve, reject) => {
const chunks: Uint8Array[] = [];
req.on("data", (chunk: Uint8Array) => {
chunks.push(chunk);
});
req.on("end", () => {
resolve(Buffer.concat(chunks));
});
req.on("error", reject);
});
};
chunks
和chunk
的类型。但是,如果您不使用打字稿,则可以删除所有类型。