-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (78 loc) · 2.71 KB
/
index.js
File metadata and controls
94 lines (78 loc) · 2.71 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const helmet = require('helmet');
const sanitize = require("./sanitize");
const WebSocketManager = require('./websocket');
const crud = require("./crud");
const app = express();
const port = 3000;
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(cors());
app.use(helmet());
const server = app.listen(port, () => {
console.log(`Open http://localhost:${port} in your browser`);
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
const wsManager = new WebSocketManager(server);
app.get('/api/v1', (req, res) => {
res.json({ api: {}, data: {}, websocket: true });
});
app.get('/data', (req, res) => {
const { tasks, links } = crud.getData();
res.json({ tasks: sanitize(tasks), links: sanitize(links) });
});
app.post("/data/task", (req, res) => {
const payload = sanitize(req.body);
const result = crud.insertTask(payload);
if (result.action === "inserted") {
wsManager.broadcast('event', { name: 'tasks', value: { type: 'add-task', task: result.item } });
}
res.status(200).json(result);
});
app.put("/data/task/:id", (req, res) => {
const id = req.params.id;
const payload = sanitize(req.body);
const result = crud.updateTask(id, payload);
if (result.action === "updated") {
wsManager.broadcast('event', { name: 'tasks', value: { type: 'update-task', task: result.item } });
}
res.status(200).json(result);
});
app.delete("/data/task/:id", (req, res) => {
const id = req.params.id;
const result = crud.deleteTask(id);
if (result.action === "deleted") {
wsManager.broadcast('event', { name: 'tasks', value: { type: 'delete-task', task: { id: Number(id) } } });
}
res.status(200).json(result);
});
// --- REST: links
app.post("/data/link", (req, res) => {
const payload = sanitize(req.body);
const result = crud.insertLink(payload);
if (result.action === "inserted") {
wsManager.broadcast('event', { name: 'links', value: { type: 'add-link', link: result.item } });
}
res.status(200).json(result);
});
app.put("/data/link/:id", (req, res) => {
const id = req.params.id;
const payload = sanitize(req.body);
const result = crud.updateLink(id, payload);
if (result.action === "updated") {
wsManager.broadcast('event', { name: 'links', value: { type: 'update-link', link: result.item } });
}
res.status(200).json(result);
});
app.delete("/data/link/:id", (req, res) => {
const id = req.params.id;
const result = crud.deleteLink(id);
if (result.action === "deleted") {
wsManager.broadcast('event', { name: 'links', value: { type: 'delete-link', link: { id: Number(id) } } });
}
res.status(200).json(result);
});