-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 994 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const { connect, addDoc, getPostById, deletePostById, editPostById } = require('./db')
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const cors = require('cors')
app.use(express.json())
app.use(cors())
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/posts', (req, res) => {
const { title, content, userId } = req.body
addDoc({ title, content, userId })
})
app.patch('/posts/:postId', (req, res) => {
const { postId, title, content } = req.body
editPostById({ postId, title, content})
})
app.delete('/posts/:postId', (req, res) => {
const { postId } = req.params
deletePostById(postId)
})
app.get('/posts', async (req, res) => {
const data = await connect()
res.send(data)
})
app.get('/posts/:postId', async (req, res) => {
const { postId } = req.params
const data = await getPostById(postId)
res.send(data)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})