-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (127 loc) · 4.03 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use-strict';
const express = require('express'),
app = express(),
fs = require('fs'),
http = require('http'),
https = require('https'),
{json, urlencoded} = require('body-parser'),
multer = require('multer'),
util = require('util'),
yaml = require('write-yaml');
const definitionFile = process.argv[2] || 'default.json'
const def = JSON.parse(fs.readFileSync(definitionFile))
const paths = def['paths'];
app.use(json({
'limit': '1mb'
}));
app.use(urlencoded({extended: true}));
const uploader = multer({dest: 'uploads/'})
paths.forEach(pathDef => {
console.log(`Adding path:`);
console.log(util.inspect(pathDef, {depth: null}));
switch (pathDef['method']) {
case 'POST':
case 'post':
method = app.post;
break;
case 'GET':
case 'get':
method = app.get;
break;
case 'PUT':
case 'put':
method = app.put;
break;
case 'DELETE':
case 'delete':
method = app.delete;
break;
case 'DEFAULT':
case 'default':
case 'ALL':
case 'all':
default:
method = app.use;
}
if (pathDef['upload']) {
app.use(pathDef['path'], uploader.any(), getHandler(pathDef));
} else {
app.use(pathDef['path'], getHandler(pathDef));
}
});
function getHandler(pathDef) {
return (req, res) => {
console.log(`Interception:`)
console.log(` Of type: ${req.method}`);
console.log(` From host: ${req.hostname}`);
console.log(` To route: ${req.originalUrl}`);
if (pathDef['parse']) {
parseRequest(req)
}
if (pathDef['response']) {
response = pathDef['response']
if (response.constructor == Array) {
responseStatus = response[Math.floor(Math.random() * response.length)]
} else if (typeof(response) === 'number') {
responseStatus = response;
} else {
responseStatus = 200;
}
} else {
responseStatus = 200;
}
if (pathDef['parse']) {
console.log(`Responding with ${responseStatus}`);
}
if (pathDef['save']) {
saveRequest(pathDef, {
headers: req.headers,
body: req.body,
params: req.params
});
}
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Headers', '*')
res.status(responseStatus).end();
};
}
function saveRequest(pathDef, data) {
filename = `${pathDef['method']}.${pathDef['path'].replace('/', '')}.${Date.now()}.${Math.floor((Math.random()*100000))}`;
console.log(`Saving to file ${filename}`);
if (pathDef['save'] === 'yaml') {
yaml.sync(`output/${filename}.yaml`, data)
} else {
fs.writeFileSync(`output/${filename}.json`, JSON.stringify(data, null, 4));
}
}
function parseRequest(req) {
console.log(` With headers:`);
console.log(util.inspect(req.headers, {depth: null}));
if (req.body) {
console.log(` With body:`);
console.log(util.inspect(req.body, {depth: null}));
}
if (req.files) {
console.log(` With files:`)
console.log(util.inspect(req.files, {depth: null}));
}
if (req.params) {
console.log(` With params:`);
console.log(util.inspect(req.params, {depth: null}));
}
if (req.query) {
console.log(` With query:`);
console.log(util.inspect(req.query, {depth: null}));
}
}
http.createServer(app).listen(def['port'], () => {
console.log(`HTTP server listening on port ${def['port']}`);
});
if (def['https']) {
const httpsdef = def['https']
const key = fs.readFileSync(httpsdef['key']);
const cert = fs.readFileSync(httpsdef['cert']);
https.createServer({key: key, cert: cert}, app).listen(httpsdef['port'], () => {
console.log(`HTTPS server listening on port ${httpsdef['port']}`);
});
}