# Next.js 14
// src/app/api/product/route.ts import prisma from '@/prisma/prisma' import type { NextApiRequest, NextApiResponse } from 'next' // GET /api/product export default async function handle(req: NextApiRequest, res: NextApiResponse) { if (req.method === 'GET') { try { const result = await prisma.products.findMany() res.status(200).json(result) } catch (error) { res.status(500).json({ error: 'Failed to fetch products' }) } } else { res.setHeader('Allow', ['GET']) res.status(405).end(`Method ${req.method} Not Allowed`) } }
# Next.js 15
import prisma from '@/prisma/prisma' import type { NextApiRequest, NextApiResponse } from 'next' export const GET = async (req: NextApiRequest, res: NextApiResponse) => { try { const result = await prisma.product.findMany(); res.status(200).json(result); } catch (error) { res.status(500).json({ error: 'Failed to fetch products' }); } };
标签:status,named,HTTP,res,prisma,Next,export,result,instead From: https://www.cnblogs.com/sekihin/p/18653438