战斗鸡 發表於 2022-4-3 17:34:00

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

<p>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.</p>
<ul>
<li>https://github.com/hoangvvo/next-connect</li>
</ul>
<div class="cnblogs_code">
<pre>import nextConnect from "next-connect"<span style="color: rgba(0, 0, 0, 1)">

const handler </span>=<span style="color: rgba(0, 0, 0, 1)"> nextConnect({
onNoMatch: (req, res) </span>=&gt; res.status(404).json({message: 'not found'<span style="color: rgba(0, 0, 0, 1)">})
})
.get((req, res) </span>=&gt; res.status(200).json({message: 'GET!'<span style="color: rgba(0, 0, 0, 1)">}))
.post((req, res) </span>=&gt; res.status(200).json({message: 'POST!'<span style="color: rgba(0, 0, 0, 1)">}))
.patch((req, res) </span>=&gt; res.status(200).json({message: 'PATCH!'<span style="color: rgba(0, 0, 0, 1)">}))
.put((req, res) </span>=&gt; res.status(200).json({message: 'PUT!'<span style="color: rgba(0, 0, 0, 1)">}))
.</span><span style="color: rgba(0, 0, 255, 1)">delete</span>((req, res) =&gt; res.status(200).json({message: 'DELETE!'<span style="color: rgba(0, 0, 0, 1)">}))

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> handler</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/Answer1215/p/16096674.html
頁: [1]
查看完整版本: [Next.js] Create an API Route Using the next-connect Package