My Next.js application includes a functional API that connects to MongoDB and Prisma. While everything runs smoothly during development, I encounter a 404 error on the API page after deploying the app with Vercel.
Below is the code for app/api/blog/route.ts:
import {db} from "@/lib/db";
import { NextResponse } from "next/server";
export async function GET(){
try{
const todos=await db.post.findMany({
orderBy:{
date:"desc",
}
});
return NextResponse.json(todos,{status:200})
}catch(error){
console.log("[GET TODO]",error)
return new NextResponse("Internal Server Error",{status:500})
}
}
The code for app/api/blog/[id]/route.ts is as follows:
import {db} from "@/lib/db";
import { NextResponse } from "next/server";
export const GET = async (request:any,{params}:any)=>{
try{
const {id}=params;
const post=await db.post.findUnique({
where:{
id
}
});
if(!post){
return NextResponse.json(
{message:"Post not found",error: "Post not found"},
{status:404}
)
}
return NextResponse.json(post)
}catch(err){
return NextResponse.json({message:"GET Error",err},{status:500})
}
}
Details from the package.json file are below:
{
"name": "portfolio",
"version": "0.1.0",
...
View the appearance at yasith.vercel.app/api/blog
I have checked environment variables and connected MongoDB Atlas to Vercel without success. Feel free to visit the deployed site [here] () I would appreciate any help as this issue prevents me from advancing in fullstack development with Next.js.