查看: 72|回覆: 0

[Next.js] Create an API Route Using the next-connect Package

[複製鏈接]

5

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-11-23
發表於 2022-4-3 17:34:00 | 顯示全部樓層 |閲讀模式

We'll learn an alternative way of creating API routes using the next-connect package. next-connect gives us an alternative that would feel familiar to anyone that has worked with Express in the past.

  • https://github.com/hoangvvo/next-connect
import nextConnect from "next-connect"

const handler = nextConnect({
  onNoMatch: (req, res) => res.status(404).json({message: 'not found'})
})
  .get((req, res) => res.status(200).json({message: 'GET!'}))
  .post((req, res) => res.status(200).json({message: 'POST!'}))
  .patch((req, res) => res.status(200).json({message: 'PATCH!'}))
  .put((req, res) => res.status(200).json({message: 'PUT!'}))
  .delete((req, res) => res.status(200).json({message: 'DELETE!'}))

export default handler

 



来源:https://www.cnblogs.com/Answer1215/p/16096674.html
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部