-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
156 lines (121 loc) · 3.36 KB
/
Copy pathserver.js
File metadata and controls
156 lines (121 loc) · 3.36 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var express = require('express');
var app = express();
var port = 3000;
app.set('port', port);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const chatHistory = [];
const nicknames = [];
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
);
// Request headers you wish to allow
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
// Pass to next layer of middleware
next();
});
// test
app.get('/', function (req, res, next) {
res.json({ message: 'hsg chat-app api works...' });
});
// history
app.get('/history', function (req, res, next) {
res.send(chatHistory);
});
app.post('/history', function (req, res, next) {
const chatMessage = req.body?.message?.toString();
const nickname = req.body?.nickname?.toString();
if (!chatMessage) {
res.status(400).send('Message is missing.');
return;
}
if (!nickname) {
res.status(400).send('Nickname is missing.');
return;
}
const date = new Date();
const message = {
id: chatHistory.length + 1,
message: chatMessage,
nickname: nickname,
createdAt: date,
};
chatHistory.push(message);
res.json(chatHistory);
});
// nicknames
app.get('/nicknames', function (req, res, next) {
res.send(nicknames);
});
app.get('/nicknames/:id', function (req, res, next) {
// simple for loop
// for (var i = 0; nicknames.length > 0; i++) {
// var nickname = nicknames[i]
// if (nickname && nickname.id === req.params.id) {
// res.send({ username: nickname.username, id: nickname.id })
// }
// }
// foreach in array
// nicknames.forEach((nickname) => {
// if (nickname && nickname.id === req.params.id) {
// res.send({ username: nickname.username, id: nickname.id })
// }
// })
// build-in .find function
const id = +req.params.id;
const nickname = nicknames.find((e) => e.id === id);
if (!nickname) {
res.status(404).send('Nickname not found.');
return;
}
res.send(nickname);
});
app.post('/nicknames', function (req, res, next) {
const userName = req.body?.nickname?.toString();
if (!userName) {
res.status(400).send('Nickname is missing.');
return;
}
if (!isNicknameUnique(userName)) {
res.status(409).send(`Nickname ${userName} already exists.`);
return;
}
const date = new Date();
const nickname = {
id: nicknames.length + 1,
nickname: userName,
createdAt: date,
};
nicknames.push(nickname);
res.json(nicknames);
});
app.delete('/nicknames/:id', function (req, res, next) {
const id = +req.params.id;
const nickName = nicknames.find((e) => e.id === id);
if (!nickName) {
res.status(404).send('Nickname not found.');
return;
}
const index = nicknames.findIndex((e) => e.id === id);
if (index < 0) {
res.status(404).send('Nickname id not found.');
return;
}
nicknames.splice(index, 1);
res.send('Nickname deleted.');
});
function isNicknameUnique(nickName) {
return !nicknames?.some((e) => e.userName === nickName);
}
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});