Last active
October 10, 2022 13:20
-
-
Save nextdev1111/cba7ea80d1064f861eea39f510f74eec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Request, Response } from "express"; | |
import { PrismaClient } from "@prisma/client"; | |
const app = express(); | |
// instance of prisma client | |
const prisma = new PrismaClient(); | |
// middlewares | |
app.use(express.json()); | |
app.get("/", async (req: Request, res: Response) => { | |
const posts = await prisma.post.findMany(); | |
return res.status(200).json({ success: true, posts }); | |
}); | |
app.post("/", async (req: Request, res: Response) => { | |
const { text, userId } = req.body; | |
const post = await prisma.post.create({ | |
data: { | |
text, | |
userId, | |
}, | |
}); | |
return res.status(201).json({ success: true, post }); | |
}); | |
app.listen(3000, () => { | |
console.log(`Listening to 3000`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment