🍣

Express & Prisma【4User 一覧取得】

2022/09/21に公開

Express & Prisma【4User 一覧取得】

YouTube: https://youtu.be/gB_lKsvv_h4

prisma crud: https://www.prisma.io/docs/concepts/components/prisma-client/crud

index.ts
import express from 'express'
import helmet from 'helmet'
import morgan from 'morgan'
import cors from 'cors'
import userRoute from './routes/user'

const app = express()
const port = 4000

app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(cors())
app.use(helmet())
app.use(morgan('common'))

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.use('/api/users', userRoute)

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
user.ts
import { Router } from "express";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient()
const router = Router()

router.get('/', async (req, res) => {
  const users = await prisma.user.findMany()
  res.status(200).json(users)
})

export default router

Discussion