-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpServer.js
More file actions
61 lines (52 loc) · 1.71 KB
/
httpServer.js
File metadata and controls
61 lines (52 loc) · 1.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
const express = require('express');
const http = require('http');
const websocket = require('ws');
const url = require('url');
const bodyParser = require('body-parser');
function httpServer(client) {
const app = express();
app.disable('x-powered-by');
app.use(bodyParser.json({ type: () => true, strict: false }));
app.get('/', (req, res) => {
res.json({ name: 'Fabric store', description: 'None', verion: '0.0.1', commit: 'None' });
});
// saveSegment
app.post('/segments', (req, res, next) => {
console.log('Saving segment');
const segment = req.body;
console.log(segment);
client.saveSegment(JSON.stringify(req.body))
.then(() => {
res.json(segment);
}).catch(next);
});
// getSegment
app.get('/segments/:linkHash', (req, res, next) => {
console.log('Getting segment');
console.log(req.url);
client.getSegment(req.params.linkHash)
.then((segment) => {
res.json(JSON.parse(segment));
}).catch(next);
});
// findSegments
app.get('/segments', (req, res, next) => {
console.log('Finding segments');
client.findSegments(url.parse(req.url).query || '')
.then((segments) => {
res.json(JSON.parse(segments));
}).catch(next);
});
// getMapIds
app.get('/maps', (req, res, next) => {
console.log('Finding maps');
client.getMapIds(url.parse(req.url).query || '')
.then((mapIds) => {
res.json(JSON.parse(mapIds) || []);
}).catch(next);
});
const server = http.createServer(app);
const wss = new websocket.Server({ server });
return server;
}
module.exports = httpServer;