-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (96 loc) · 3.19 KB
/
server.js
File metadata and controls
108 lines (96 loc) · 3.19 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
const express = require('express');
const bodyParser = require('body-parser');
const lodash = require('lodash');
const constants = require('./constants');
const morgan = require('morgan');
const Server = function Server(db, rest, prefix) {
const watcher = {};
const urlRegex = RegExp(`/+${prefix}/([^/?]+).*`);
// Used to callback the client which requested a long polling
// on the affected resource.
function publish(resource) {
if (watcher[resource]) {
watcher[resource].forEach((res) => {
// Trigger a list response on the waiting clients
rest.list(resource, {}, res);
});
// Once watchers are called back, remove them from the waiting list
watcher[resource].length = 0;
}
}
// Create the Express application
const app = express();
app.disable('x-powered-by');
// For logging (TODO: Add an option)
app.use(morgan('combined'));
// For CORS (TODO: Add an option)
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
//intercepts OPTIONS method
if ('OPTIONS' === req.method) {
//respond with 200
res.sendStatus(200);
}
else {
next();
}
});
app.use(bodyParser.json());
// Define the application routes
app.get(`/+${prefix}/[^/]+/`, (req, res) => {
const resource = urlRegex.exec(req.url)[1];
if (lodash.has(req.query, 'watch')) {
watcher[resource] = watcher[resource] || [];
watcher[resource].push(res);
} else {
rest.list(resource, req.query, res);
}
});
app.get(`/+${prefix}/*/:id`, (req, res) => {
const resource = urlRegex.exec(req.url)[1];
rest.retrieve(resource, req.params.id, req.query, res);
});
app.post(`/+${prefix}/[^/]+/`, (req, res) => {
const resource = urlRegex.exec(req.url)[1];
rest.create(resource, req.body, req.query, res);
publish(resource);
});
app.post(`/+${prefix}/*/:id`, (req, res) => {
const resource = urlRegex.exec(req.url)[1];
rest.update(resource, req.params.id, req.body, req.query, res);
publish(resource);
});
app.delete(`/+${prefix}/*/:id`, (req, res) => {
const resource = urlRegex.exec(req.url)[1];
rest.del(resource, req.params.id, res);
publish(resource);
});
app.all('*', (req, res) => {
res.status(404).send({ error: `Unknown URL ${req.url}` });
});
// Error handler
app.use((err, req, res, next) => {
console.error('Error:', err.stack);
next(err);
});
app.use((err, req, res, next) => {
res.status(500).send({ error: 'Server Error' });
});
return {
listen: function listen(options) {
app.listen(options.port, options.host, () => {
let prompt = 'server listening on';
prompt += ` http://${options.host}:${options.port}/${options.prefix}`;
if (options.storage) {
prompt += ` using filesystem store on ${options.storage}`;
} else {
prompt += ' using memory store';
}
console.log(prompt);
});
},
};
};
module.exports = Server;