-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (43 loc) · 1.6 KB
/
index.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
'use strict';
const log = require('fancy-log');
const color = require('ansi-colors');
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const glob = require('glob');
glob('routes/*.json', {absolute: true}, (err, files) => {
const portApis = files.reduce((groups, path) => {
try {
const json = JSON.parse(fs.readFileSync(path, 'utf8'));
const group = groups[json.port] || [];
group.push(json);
groups[json.port] = group;
} catch(e) {}
return groups;
}, {});
const portEntries = Object.entries(portApis);
for (const [port, routes] of portEntries) {
const app = express();
app.use(cors());
log.info(color.grey(`configuring app at ${port}`));
routes.forEach(route => {
log.info(color.grey(`- ${route.path}`));
// Accepts both GET and POST requests
app.all(route.path, (req, res) => {
res.set('Content-Type', 'application/json');
const queries = route.queries || [];
queries: for (const {params, response} of queries) {
for (const key in params) {
if (params[key] !== req.query[key]) {
continue queries;
}
}
return res.send(JSON.stringify(response));
}
return res.send(JSON.stringify(route.response));
});
});
log.info(`Starting server at ${port}\n`);
app.listen(port);
}
});