-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
132 lines (117 loc) · 3.69 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
'use strict';
// const fs = require('fs');
const config = 'tbd_commandconfig';
const express = require('express');
const cors = require('cors');
const vhost = require('vhost');
const connectRoute = require('connect-route');
// const send = require('connect-send-json').json();
const bodyParser = require('body-parser');
// const error = require('connect-send-error').error();
const _ = require('lodash');
{
const server = express();
const api = express();
const vhostname = config.hostname || 'yourFunny.domain.de';
const port = config.port || 3081;
const baseUrl = config.baseUrl || '/api/v1';
let mocked = {};
// http://underscorejs.org/#extend
// var result={};
// Object.keys(obj1).forEach((key) => result[key] = obj1[key]);
// Object.keys(obj2).forEach((key) => result[key] = obj2[key]);
// method({
// ...object1,
// ...object2
// })
// const https = require('https');
// const options = {
// key: fs.readFileSync('ssl/key.pem'),
// cert: fs.readFileSync('ssl/cert.pem'),
// ca: fs.readFileSync('ssl/ca.crt')
// };
const mockedRoute = (req, res, next) => {
if (typeof mocked[req.params.call] !== 'undefined') {
res.statusCode = mocked[req.params.call].httpStatus;
res.send(mocked[req.params.call].mockResponse);
}
};
// https.createServer(server).listen(port);
// https.createServer(options,server).listen(port);
server.listen(port, () => {
console.log('Mock\'n\'Rolling on port ' + port + ' ...');
// console.log(process.argv);
process.argv.forEach((val, index, array) => {
console.log(index + ': ' + val);
});
});
server.use(vhost(vhostname, api));
server.use(vhost('localhost', api));
api.use(cors());
// api.use(error);
// api.use(send);
api.use(connectRoute((router) => {
router.get(baseUrl + '/*/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.get(baseUrl + '/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.post(baseUrl + '/*/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.post(baseUrl + '/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.put(baseUrl + '/*/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.put(baseUrl + '/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.delete(baseUrl + '/*/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.delete(baseUrl + '/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.patch(baseUrl + '/*/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
router.patch(baseUrl + '/:call/', (req, res, next) => {
mockedRoute(req, res, next);
next();
});
// HEAD?
api.use(bodyParser.json());
router.post('/mock/configure/:call/:httpStatus', (req, res, next) => {
const mockedCallName = req.params.call;
const mockedStatusCode = req.params.httpStatus;
const curCall = { [mockedCallName]: {
httpStatus: mockedStatusCode, mockResponse: req.body } };
// http://stackoverflow.com/questions/19965844/lodash-difference-between-extend-assign-and-merge
mocked = _.extend(
mocked,
{ [mockedCallName]: {
httpStatus: mockedStatusCode, mockResponse: req.body } });
res.statusCode = 200;
res.send(mocked);
next();
});
router.delete('/mock/configure', (req, res, next) => {
mocked = {};
res.statusCode = 200;
res.send();
next();
});
}));
}