-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
65 lines (55 loc) · 1.57 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
const express = require(`express`);
const cors = require(`cors`);
let responseData = {
logs: [],
};
const app = express();
app.use(express.json());
app.use(cors({
credentials: true,
headers: `Origin, X-Requsted-With, Content-Type, Accept`,
methods: `PUT, PATCH, GET, POST, DELETE, OPTIONS`,
origin: `*`,
}));
app.use('/', express.static(`public`));
app.get(`/json`, (req, res) => {
res.set(`Content-Type`, `application/json`);
res.writeHead(200);
res.end(JSON.stringify(responseData));
});
app.get(`/add`, (req, res) => {
res.set(`Content-Type`, `application/json`);
const message = req.query[`message`];
const senderContext = req.query[`sender`];
if (typeof message !== `undefined` && typeof senderContext !== `undefined`) {
const timestamp = Date.now();
responseData.logs.push({
message,
senderContext,
timestamp,
});
res.writeHead(200);
res.end(JSON.stringify({
error: false,
message: `Success.`,
}));
} else {
console.log('fail');
res.writeHead(500);
res.end(JSON.stringify({
error: true,
message: `Unable to add log.`,
}));
}
});
app.post(`/clear`, (req, res) => {
res.set(`Content-Type`, `application/json`);
responseData.logs = [];
res.writeHead(200);
res.end(JSON.stringify({
error: false,
message: `Success.`,
}));
});
const startup = () => console.log(`Listening for requests at http://localhost:3000`);
app.listen(3000, startup);