This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
122 lines (96 loc) · 3.17 KB
/
server.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const pkg = require('./package.json');
const express = require('express');
const expressHandlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors');
const morgan = require('morgan');
const compression = require('compression');
const moment = require('moment');
const faker = require('faker');
const fsp = require('fs-promise');
const path = require('path');
const URL = require('url');
const chalk = require('chalk');
const mime = require('mime-types');
const birthDate = moment().toString();
const PORT = 3000;
const app = express();
app.use(helmet({
noCache: true
}));
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());
app.use(compression());
app.use(morgan('dev'));
app.engine('.hbs', expressHandlebars({
defaultLayout: 'main',
extname: '.hbs',
helpers: {
to_state(str) {
switch (str.toUpperCase()) {
case 'GET': return 'success';
case 'POST': return 'primary';
case 'PUT': return 'warning';
case 'DELETE': return 'danger';
case 'OPTIONS': return 'info';
default: return 'default';
}
}
}
}));
app.set('view engine', '.hbs');
app.use((request, response, next) => {
// Consistent results everyday
const seed = parseFloat(moment().startOf('day').format('X'));
faker.seed(seed);
next();
});
app.use(express.static('static'));
app.get('/', (request, response) => {
const {host} = request.headers;
const availableRoutes = endpoints
.reduce((previous, endpoint) => [...previous, ...endpoint.stack], []) // registered routes
.filter(r => r.route) // take out all the middleware
.map(r => {
const methods = Object.keys(r.route.methods)
.filter((method) => r.route.methods[method])
.map((method) => method.toUpperCase());
return {
path: r.route.path,
methods,
};
});
response.render('listing', {
birthDate,
version: pkg.version,
host,
availableRoutes
});
});
let endpoints = [];
(async function () {
for (let endpointPath of await fsp.readdir('./endpoints')) {
const fullPath = path.resolve('./endpoints', endpointPath);
const stat = await fsp.stat(fullPath);
if (!stat.isFile()) continue;
if (mime.lookup(endpointPath) !== 'application/javascript') continue;
const endpoint = require(fullPath);
app.use('/', endpoint);
endpoints.push(endpoint);
}
// Handle 404
app.use((request, response) => {
response.status(404).type('text').send(`Endpoint "${request.method} ${request.url}" is not yet configured`);
});
})();
const server = app.listen(PORT, () => {
const port = server.address().port;
let address = server.address().address;
address = address === '::' ? `[${address}]` : address;
const url = URL.parse(`http://${address}:${port}`).href;
console.log(`Fake backend listening on ${chalk.green(url)}`)
});