-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
72 lines (63 loc) · 2.02 KB
/
app.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
const express = require('express');
const package = require('./package.json');
const cookieParser = require('cookie-parser');
const moment = require('moment');
const program = require('commander');
const prettyjson = require('prettyjson');
require('node-json-color-stringify');
// Commander - setup command-line args
program
.name(package.name)
.version(package.version)
.option('-p, --port <number>', 'port to run mock-server on', 5555)
.option('-j, --json', 'output data in json format', false)
.option('-c, --nocolor', 'do not use colors', false)
.parse(process.argv);
// Commander - output help info
program.outputHelp();
// Create app
var app = express();
// Add request time in
app.use(function(req, res, next) {
req.requestTime = moment();
next();
});
// Other middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// Capture all methods to all routes
app.all('*', function(req, res, next) {
const responseTime = moment();
const ip = req.ip ? req.ip : req.connection.remoteAddress;
const requestLog = {
requestTime: req.requestTime.toISOString(),
responseTime: responseTime.toISOString(),
remoteAddress: ip,
method: req.method,
httpVersion: req.httpVersion,
originalUrl: req.originalUrl,
pathName: req._parsedUrl.pathname,
queryString: req._parsedUrl.query,
query: req.query,
headers: req.headers
};
res.send('OK');
console.log('\n============================================');
if (program.json === true && program.nocolor === false) {
// JSON with color
console.log(JSON.colorStringify(requestLog, null, 2));
} else if (program.json === true && program.nocolor === true) {
// JSON with no color
console.log(JSON.stringify(requestLog, null, 2));
} else {
// Pretty print with/without color
console.log(
prettyjson.render(requestLog, {
noColor: program.nocolor
})
);
}
});
// export the express app and program (command-line options)
module.exports = { app, program };