docs/1.30/get-started/03-build-rest-apis-with-prisma-JAVASCRIPT-e002.mdx
import QueryChooser from 'components/Markdown/QueryChooser'
export const meta = { title: 'Build an App', position: 3, gettingStartedOrder: 2, gettingStartedTitle: 'REST API', nextText: 'Congratulations! 🚀 You made it through the quickstart tutorial and learned how to use Prisma and the Prisma client to build a REST API.', technology: 'node', technologyOrder: 1, articleGroup: 'Build an App', }
On this page, you will learn how to:
curlYou'll use Express.js as your web server. Add the express and body-parser dependencies to your project with this command:
npm install --save express body-parser
There will be six routes that provide the API for a simple blogging application:
GET
/posts/published: Returns all published posts/post/:postId: Returns a specific post by its id/posts/user/:userId: Returns all the post written by a specific userPOST
/user: Create a new user/post/draft: Create a new unpublished postPUT
/post/publish: Publish a postReplace the current contents of index.js with the following code:
const { prisma } = require('./generated/prisma-client')
const app = express()
const express = require('express')
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.get(`/posts/published`, async (req, res) => {
const publishedPosts = await prisma.posts({ where: { published: true } })
res.json(publishedPosts)
})
app.get('/post/:postId', async (req, res) => {
const { postId } = req.params
const post = await prisma.post({ id: postId })
res.json(post)
})
app.get('/posts/user/:userId', async (req, res) => {
const { userId } = req.params
const postsByUser = await prisma
.user({ id: userId })
.posts()
res.json(postsByUser)
})
app.post('/user', async (req, res) => {
const newUser = await prisma.createUser(req.body)
res.json(newUser)
})
app.post('/post/draft', async (req, res) => {
const newPost = await prisma.createPost(req.body)
res.json(newPost)
})
app.put(`/post/publish/:postId`, async (req, res) => {
const { postId } = req.params
const updatedPost = await prisma.updatePost(
{
where: { id: postId },
data: { published: true },
},
)
res.json(updatedPost)
})
app.listen(3000, () => console.log('Server is running on http://localhost:3000'))
Start the server with this command:
node index.js
You can now use a tool like curl or Postman to explore your the functionlity of the routes.
curl<QueryChooser titles={["Create new draft", "Publish a draft", "Fetch post by ID", "Create user"]}>
curl -X POST \\
http://localhost:3000/post/draft \\
-H 'Content-Type: application/json' \\
-d '{
"title": "Awesome Post"
}'
curl -X PUT \\
http://localhost:3000/post/publish \\
-H 'Content-Type: application/json' \\
-d '{
"id": "__POST_ID__"
}'
curl -X GET \\
http://localhost:3000/post/__POST_ID__
curl -X POST \\
http://localhost:3000/user \\
-H 'Content-Type: application/json' \\
-d '{
"name": "Alice"
}'
In some snippets, you need to replace the
__USER__ID__or__POST_ID__placeholder with the ID of an actual user.