forked from ShivrajRath/Node-Mock-REST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (134 loc) · 4.78 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*jshint node: true*/
/* global exports, module, process */
/**
* Index.js
* Creates an express app to manage mock response
*/
var express = require('express'),
fs = require('fs'),
path = require('path'),
bodyParser = require('body-parser');
var defaultConfig = require('./config');
var constants = require('./constants.json');
var _ = require('lodash');
(function () {
var node_rest_mock = {};
exports.startMock = node_rest_mock.startMock = function (config, app) {
if (!app) {
app = createApp();
}
// merge the config with the default config
config = _.merge(defaultConfig, config);
addGETStubs(config, app);
addPOSTStubs(config, app);
// Don't want these for an existing express app
if (!module.parent) {
catchAll(app);
start(config, app);
}
};
// kick start the server when run standalone
if (!module.parent) {
node_rest_mock.startMock();
}
})();
function addGETStubs(config, app) {
app.get('/stubService/*', function (req, res) {
try {
var call_param = req.params[0];
// Send file content as response
res.setHeader('Content-Type', 'application/json');
var filePath = process.cwd() + path.sep + config.stub_dir + path.sep + call_param + '.json';
res.sendFile(filePath, {}, function (err) {
res.status(404).end(JSON.stringify(constants.ERR.File_Not_Found));
});
} catch (ex) {
res.status(404).end(ex.message);
}
});
}
function addPOSTStubs(config, app) {
app.post('/stubService/*', function (req, res) {
var reqPath, respPath, x, y;
try {
var call_param = req.params[0];
reqPath = process.cwd() + path.sep + config.stub_dir + path.sep + call_param + '.json';
/** finds the response path*/
x = call_param.split('/');
x[x.length - 1] = 'postresp/' + x[x.length - 1];
respPath = process.cwd() + path.sep + config.stub_dir + path.sep + x.join('/') + '.json';
// Writes the request body to the file
fs.writeFile(reqPath, JSON.stringify(req.body), function (err) {
if (err) {
// NodeJS can create a file, it needs the directory to be present
if (err.code === 'ENOENT') {
res.status(500).end(JSON.stringify(constants.ERR.ENOENT));
} else {
res.status(500).end(err.toString());
}
} else {
fs.exists(respPath, function (exists) {
if (exists) {
// If response path exists, send file as response
res.sendFile(respPath, {}, function (err) {
res.status(404).end(JSON.stringify(constants.ERR.File_Not_Found));
});
} else {
// If reponse path doesn't exists, send success response
res.status(200).end(JSON.stringify(constants.SUCCESS));
}
});
}
});
} catch (ex) {
res.status(404).end(ex.message);
}
});
}
function start(config, app) {
// Run the application on the port
var port = config.server_port;
if (app.get('env')) {
port = process.env.PORT || config.server_port;
}
// Starts the app
app.listen(port, function () {
console.log('App Started at port number ' + port);
});
}
function createApp() {
// Creates express app
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}));
// parse application/json
app.use(bodyParser.json());
/**
* Allows access to all origin
*/
app.all("*", function (req, res, next) {
// Specify the origin if you want to control the CORS origin
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
// Uncomment to allow with credentials. You'd need to give specific origin in that case
//res.header('Access-Control-Allow-Credentials', true);
next();
});
return app;
}
/**
* Sucess response for all other requests
* @param {Express} app Express App
*/
function catchAll(app) {
app.all('*', function (req, res) {
try {
res.status(200).end(JSON.stringify(constants.SUCCESS));
} catch (ex) {
res.status(404).end(ex.message);
}
});
}