-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
141 lines (120 loc) · 3.15 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express');
const app = express();
const cors = require('cors');
const multer = require('multer');
const uniqid = require('uniqid');
let posts = require('./posts');
const getPostById = id => posts.find(post => post.id === id);
const PORT = 9000;
app.use(express.static('public'));
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
/* Setup File upload */
const upload = multer({
limits: { fileSize: 5 * 1024 * 1024 },
storage: multer.diskStorage({
destination(req, file, cb) {
// public/img 폴더에 파일을 저장한다. public/img 폴더가 존재해야 한다.
cb(null, 'public/img/');
},
filename(req, file, cb) {
// 전송된 파일 자신의 이름으로 파일을 저장한다.
cb(null, file.originalname);
},
}),
});
// mainPage
app.get('/posts', (req, res) => {
const { nation } = req.query;
res.send(posts.filter(post => post.nation === nation));
});
app.patch('/post/liked/:id', (req, res) => {
const { id } = req.params;
const postById = getPostById(id);
if (!postById?.id) {
res.status(404).send('해당 post가 존재하지 않습니다.');
return;
}
posts = posts.map(post => (post.id === id ? { ...post, liked: !post.liked } : post));
res.send();
});
// uploadPage
app.post('/upload', upload.single('img'), (req, res) => {
console.log('UPLOAD SUCCESS!', req.file);
res.json({ success: true, file: req.file });
});
app.post('/post', (req, res) => {
const newPost = {
...req.body,
id: uniqid(),
userId: 'admin',
liked: false,
comments: [],
};
posts = [...posts, newPost];
console.log(newPost.id);
res.status(201).send(newPost.id);
});
app.patch('/post/:id', (req, res) => {
const { id } = req.params;
const postById = getPostById(id);
if (!postById?.id) {
res.status(404).send('해당 post가 존재하지 않습니다.');
return;
}
posts = posts.map(post =>
post.id === id
? {
...post,
...req.body,
}
: post
);
res.send(id);
});
// detailPage
app.get('/post/:id', (req, res) => {
const { id } = req.params;
const postById = getPostById(id);
console.log(id, postById);
if (!postById.id) {
res.status(404).send('해당 post가 존재하지 않습니다.');
return;
}
res.send(postById);
});
app.post('/post/comment/:id', (req, res) => {
const { id } = req.params;
const postById = getPostById(id);
if (!postById?.id) {
res.status(404).send('해당 post가 존재하지 않습니다.');
return;
}
const newComment = {
id: uniqid(),
...req.body,
};
posts = posts.map(post =>
post.id === id
? {
...post,
comments: [...post.comments, newComment],
}
: post
);
res.send(newComment);
});
app.delete('/post/:id', (req, res) => {
const { id } = req.params;
const postById = getPostById(id);
if (!postById?.id) {
res.status(404).send('해당 post가 존재하지 않습니다.');
return;
}
posts = posts.filter(post => post.id !== id);
res.send();
});
app.listen(PORT, () => {
console.log(`Server listening at http://localhost:${PORT}`);
});