Skip to content

Instantly share code, notes, and snippets.

@ethanmick
Created February 24, 2023 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanmick/ba361e93d1233ad9214af2b7228fb950 to your computer and use it in GitHub Desktop.
Save ethanmick/ba361e93d1233ad9214af2b7228fb950 to your computer and use it in GitHub Desktop.
Next.js App API Route
import { prisma } from '@/lib/db'
import { NextResponse } from 'next/server'
export async function GET(request: Request) {
console.log('** GET /books')
const books = await prisma.book.findMany()
return NextResponse.json(books)
}
export async function POST(request: Request) {
console.log('** POST /books')
const { name, author } = await request.json()
const book = await prisma.book.create({
data: {
name,
author
}
})
return NextResponse.json(book)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment