zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

JSAPI The Using an create connect package
2023-09-14 08:59:12 时间

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