Route workin in Dev and not Prod?
Hey guys I have a route defined (code below) that in dev works fine, I call it with PUT and it does just that fine. I am using latest NextJS 14 and am hosgting prod env in Vercel. In prod I get:
But the same call works perfectly fine in dev
Request URL:
https://xxxxxx/api/defendants?id=13
Request Method:
PUT
Status Code:
405 Method Not Allowed
Remote Address:
xxxxxxx
Referrer Policy:
strict-origin-when-cross-origin
Request URL:
https://xxxxxx/api/defendants?id=13
Request Method:
PUT
Status Code:
405 Method Not Allowed
Remote Address:
xxxxxxx
Referrer Policy:
strict-origin-when-cross-origin
Request URL:
http://localhost:3000/api/defendants?id=210
Request Method:
PUT
Status Code:
200 OK
Remote Address:
[::1]:3000
Referrer Policy:
strict-origin-when-cross-origin
Request URL:
http://localhost:3000/api/defendants?id=210
Request Method:
PUT
Status Code:
200 OK
Remote Address:
[::1]:3000
Referrer Policy:
strict-origin-when-cross-origin
// /api/defendants.js
import { NextRequest, NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export async function GET() {
try {
const defendants = await prisma.defendant.findMany({
select: {
id: true,
companyName: true,
companyAddress: true,
ownerName: true,
ownerAddress: true,
ownerPhone: true,
ownerEmail: true,
},
});
return NextResponse.json({ defendants });
} catch (error) {
console.error("Error fetching defendants:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
} finally {
await prisma.$disconnect();
}
}
export async function PUT(request: NextRequest) {
try {
const data = await request.json();
const updatedDefendant = await prisma.defendant.update({
where: { id: data.id },
data: {
companyName: data.companyName,
companyAddress: data.companyAddress,
ownerName: data.ownerName,
ownerAddress: data.ownerAddress,
ownerPhone: data.ownerPhone,
ownerEmail: data.ownerEmail,
},
});
return NextResponse.json({ defendant: updatedDefendant });
} catch (error) {
console.error("Error updating defendant:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
} finally {
await prisma.$disconnect();
}
}
// /api/defendants.js
import { NextRequest, NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export async function GET() {
try {
const defendants = await prisma.defendant.findMany({
select: {
id: true,
companyName: true,
companyAddress: true,
ownerName: true,
ownerAddress: true,
ownerPhone: true,
ownerEmail: true,
},
});
return NextResponse.json({ defendants });
} catch (error) {
console.error("Error fetching defendants:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
} finally {
await prisma.$disconnect();
}
}
export async function PUT(request: NextRequest) {
try {
const data = await request.json();
const updatedDefendant = await prisma.defendant.update({
where: { id: data.id },
data: {
companyName: data.companyName,
companyAddress: data.companyAddress,
ownerName: data.ownerName,
ownerAddress: data.ownerAddress,
ownerPhone: data.ownerPhone,
ownerEmail: data.ownerEmail,
},
});
return NextResponse.json({ defendant: updatedDefendant });
} catch (error) {
console.error("Error updating defendant:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
} finally {
await prisma.$disconnect();
}
}
3 Replies