-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (26 loc) · 748 Bytes
/
index.js
File metadata and controls
35 lines (26 loc) · 748 Bytes
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
const express = require('express');
const http = require('http');
const socketio = require('socket.io');
const PORT = process.env.PORT || 1337;
const data = [{}];
const app = express();
const server = http.Server(app);
const io = socketio(server);
app
.use(express.json())
.post('/q', (req, res) => {
const { body } = req;
data.push(body);
console.log('Received new data:');
console.log(body);
io.emit('new data', body);
res.status(201).json({ status: 'created' });
})
.get('/q', (req, res) => {
res.json({ data });
})
.get('/q/latest', (req, res) => {
res.json(data[data.length - 1]);
})
.use(express.static('./public'))
server.listen(PORT, () => { console.log(`Listening on :${PORT}`); })