#Thread
1 messages · Page 1 of 1 (latest)
Hi @rough kettle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
@peak pulsar Give me an example of a DELTE route handler
Sorry @rough kettle, I seem to be having a bit of trouble. Don't worry though, I've pinged my supprt and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
Let me check the knowledge base for you. One moment! :books:
Here's an example of a DELETE route handler from the provided extracts:
// pages/api/post/[id].ts
import prisma from '../../../lib/prisma';
// DELETE /api/post/:id
export default async function handle(req, res) {
const postId = req.query.id;
if (req.method === 'DELETE') {
const post = await prisma.post.delete({
where: { id: postId },
});
res.json(post);
} else {
throw new Error(
`The HTTP ${req.method} method is not supported at this route.`,
);
}
}
This example is from a guide on building a full-stack app with Next.js, Prisma, and Vercel Postgres. The route handler is responsible for deleting a post with a given id using the Prisma Client source (https://vercel.com/guides/nextjs-prisma-postgres).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@rough kettle kapa.ai is still learning and improving, please let us how it did by reacting below
@peak pulsar not in pages but a route handler in api folder.
Let me check the knowledge base for you. One moment! :books:
I apologize for the confusion. Based on the provided extracts, there isn't a specific example of a DELETE route handler using the app directory. However, you can create a custom route handler in the app directory by following the general conventions for route handlers.
Here's an example of how you can create a DELETE route handler in the app directory:
-
Create a
route.tsfile inside theapp/api/postdirectory. -
Add the following code to the
route.tsfile:
// app/api/post/[id]/route.ts
import prisma from '../../../lib/prisma';
export async function DELETE(request: Request) {
const postId = request.url.split('/').pop();
const post = await prisma.post.delete({
where: { id: postId },
});
return new Response(JSON.stringify(post), { status: 200 });
}
This example assumes you have a prisma instance set up for handling database operations. The DELETE function retrieves the id of the Post record from the URL and uses Prisma Client to delete this record in the database.