Skip to content

Instantly share code, notes, and snippets.

@nextdev1111
Last active October 10, 2022 13:20
Show Gist options
  • Save nextdev1111/cba7ea80d1064f861eea39f510f74eec to your computer and use it in GitHub Desktop.
Save nextdev1111/cba7ea80d1064f861eea39f510f74eec to your computer and use it in GitHub Desktop.
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