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

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

 

posted @ 2022-04-03 17:34  Zhentiw  阅读(37)  评论(0编辑  收藏  举报