This repository has been archived by the owner on Jun 19, 2019. It is now read-only.
forked from mendhak/docker-http-https-echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (70 loc) · 1.79 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var express = require('express')
const morgan = require('morgan');
var http = require('http')
var https = require('https')
var app = express()
const os = require('os');
const jwt = require('jsonwebtoken');
var concat = require('concat-stream');
app.set('json spaces', 2);
app.use(morgan('combined'));
app.use(function(req, res, next){
req.pipe(concat(function(data){
req.body = data.toString('utf8');
next();
}));
});
app.all('*', (req, res) => {
const echo = {
path: req.path,
headers: req.headers,
method: req.method,
body: req.body,
cookies: req.cookies,
fresh: req.fresh,
hostname: req.hostname,
ip: req.ip,
ips: req.ips,
protocol: req.protocol,
query: req.query,
subdomains: req.subdomains,
xhr: req.xhr,
os: {
hostname: os.hostname()
}
};
if (process.env.JWT_HEADER) {
const token = req.headers[process.env.JWT_HEADER.toLowerCase()];
if (!token) {
echo.jwt = token;
} else {
const decoded = jwt.decode(token, {complete: true});
echo.jwt = decoded;
}
}
res.json(echo);
console.log('-----------------')
console.log(echo);
});
const sslOpts = {
key: require('fs').readFileSync('privkey.pem'),
cert: require('fs').readFileSync('fullchain.pem'),
};
http.createServer(app).listen(80);
https.createServer(sslOpts,app).listen(443);
let calledClose = false;
process.on('exit', function () {
if (calledClose) return;
console.log('Got exit event. Trying to stop Express server.');
server.close(function() {
console.log("Express server closed");
});
});
process.on('SIGINT', function() {
console.log('Got SIGINT. Trying to exit gracefully.');
calledClose = true;
server.close(function() {
console.log("Exoress server closed. Asking process to exit.");
process.exit()
});
});