-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
76 lines (59 loc) · 1.71 KB
/
server.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const server = express();
// server.use(express.static('/client/client.js'))
// server.use(express.static('/client/index.html'))
server.use(cors());
server.use(express.json())
server.use(bodyParser.text({
extended: true,
limit: '50mb',
parameterLimit: 10000000
}));
server.use(bodyParser.urlencoded({
extended: true,
limit: '50mb',
parameterLimit: 10000000
}))
server.use(bodyParser.json({
limit: '50mb',
parameterLimit: 10000000
}))
server.use(bodyParser.raw({
limit: '50mb',
inflate: true,
parameterLimit: 10000000
}))
// My blogs resource
const blog = []
const port = process.env.PORT || 3000;
// root route
server.get('/', (req, res) => res.send('Hello, client!'))
//search route
server.get('/blogs', (req, res)=> res.send(JSON.stringify({ blog })))
// Create new blog post route
server.post('/blog', (req, res) => {
const newBlog = JSON.parse(req.body);
blog.push(newBlog);
res.send(JSON.stringify(newBlog))
})
server.get('/blog/comments', (req, res)=> {
res.send(JSON.stringify(blog[0].comments))
})
server.post('/blog/comments', (req, res) => {
const newComment= JSON.parse(req.body);
blog[newComment.id].comments.push(newComment);
res.send(JSON.stringify(newComment))
})
server.post('/blog/emojis', (req, res) => {
const newEmoji = JSON.parse(req.body)
blog[newEmoji.id].emojiCount = newEmoji;
res.send(JSON.stringify(newEmoji))
})
server.delete('/blog', (req, res)=> {
const deletePost = JSON.parse(req.body);
blog.splice(deletePost.id,1)
res.send(JSON.stringify(deletePost))
})
server.listen(port, () => console.log(`Express now departing from http://localhost:${port}`))