-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (40 loc) · 1.16 KB
/
app.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
43
44
45
46
47
48
49
50
51
'use strict'
const express = require('express')
const path = require('path')
const BlogStorage = require('./BlogStorage')
const app = express()
const port = 8000
const blogStorage = new BlogStorage()
app.use(express.static(path.join(__dirname, 'static')))
app.use(express.json())
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'static', 'html', 'index.html'))
})
app.get('/post', (req, res) => {
const id = req.query.id
const post = blogStorage.getPost(id)
if (!post) {
return res.status(404).json({ message: 'Post Not Found' })
}
res.json(post)
})
app.get('/posts', (req, res) => {
const page = req.query.page || 0
const size = req.query.size || 3
const posts = blogStorage.getPosts(page, size)
res.json(posts)
})
app.post('/post/new', (req, res) => {
const post = req.body
const createdPost = blogStorage.addPost(post)
res.json(createdPost)
})
app.post('/comment/new', (req, res) => {
const postId = req.query.postId
const comment = req.body
const addedComment = blogStorage.addComment(postId, comment)
res.json(addedComment)
})
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
})