泥蛋 發表於 2025-1-5 16:10:00

Export a named export for each HTTP method instead.(Next.js 15)

<strong># Next.js 14</strong><br>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> src/app/api/product/route.ts</span>
import prisma from '@/prisma/prisma'<span style="color: rgba(0, 0, 0, 1)">
import type { NextApiRequest, NextApiResponse } from </span>'next'

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> GET /api/product</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> async <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> handle(req: NextApiRequest, res: NextApiResponse) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (req.method === 'GET'<span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
      const result </span>=<span style="color: rgba(0, 0, 0, 1)"> await prisma.products.findMany()
      res.status(</span>200<span style="color: rgba(0, 0, 0, 1)">).json(result)
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
      res.status(</span>500).json({ error: 'Failed to fetch products'<span style="color: rgba(0, 0, 0, 1)"> })
    }
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
    res.setHeader(</span>'Allow', ['GET'<span style="color: rgba(0, 0, 0, 1)">])
    res.status(</span>405<span style="color: rgba(0, 0, 0, 1)">).end(`Method ${req.method} Not Allowed`)
}
}</span></pre>
</div>
<p><strong># Next.js 15</strong></p>
<div class="cnblogs_code">
<pre>import prisma from '@/prisma/prisma'<span style="color: rgba(0, 0, 0, 1)">
import type { NextApiRequest, NextApiResponse } from </span>'next'<span style="color: rgba(0, 0, 0, 1)">

export const GET </span>= async (req: NextApiRequest, res: NextApiResponse) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
    const result </span>=<span style="color: rgba(0, 0, 0, 1)"> await prisma.product.findMany();
    res.status(</span>200<span style="color: rgba(0, 0, 0, 1)">).json(result);
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
    res.status(</span>500).json({ error: 'Failed to fetch products'<span style="color: rgba(0, 0, 0, 1)"> });
}
};</span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <br /><p><table><tr><td><img style="border-top-width: 0pt; border-left-width: 0pt; border-bottom-width: 0pt; border-right-width: 0pt" alt="Creative Commons License" src="http://i.creativecommons.org/l/by-nc/2.5/cn/88x31.png" /> </td><td>本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 </td></p><br><br>
来源:https://www.cnblogs.com/sekihin/p/18653438
頁: [1]
查看完整版本: Export a named export for each HTTP method instead.(Next.js 15)