Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·191 lines (186 loc) · 6.78 KB
/
Copy pathindex.js
File metadata and controls
executable file
·191 lines (186 loc) · 6.78 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* * Create HTTP GET POST PUT DELETE methods using express for easier REST calls.
**/
var express = require('express')
var http = require('http')
var https = require('https')
var path = require('path')
var multer = require('multer')
var server;
var bodyParser = require('body-parser')
var app = express()
var rport;
var rports;
//************************************************************************************************
/** Utility function to get the right type of body parser for handling POST requests.**/
function getContentType(options) {
if (options.contentType == "urlencoded") {
return bodyParser.urlencoded(options.config)
} else if (options.contentType == "json") {
return bodyParser.json(options.config)
} else if (options.contentType == "raw") {
return bodyParser.raw(options.config)
} else if (options.contentType == "text") {
return bodyParser.text(options.config)
}
}
//************************************************************************************************
module.exports = {
/**
GET function which takes a route on which the call needs to act and also the Value to return
**/
get: function(onPath, returnValueOrFunc) {
app.get(onPath, function(req, res) {
try {
var contentType = req.get('content-type') || "None";
if (typeof returnValueOrFunc == 'function') {
res.send(returnValueOrFunc(false, req.query, contentType));
} else {
res.send(JSON.stringify(returnValueOrFunc))
}
} catch (e) {
if (typeof returnValueOrFunc == 'function') {
returnValueOrFunc(e, "", "");
} else {
throw e;
}
}
});
},
/**
GET function which takes a route on which the call needs to act and also the File to return
**/
getfile: function(onPath, returnFileFunc) {
app.get(onPath, function(req, res) {
try {
if (typeof returnFileFunc == 'function') {
res.sendFile(path.join(__dirname, '../../', returnFileFunc(false, req.query)));
} else {
res.sendFile(path.join(__dirname, '../../', returnFileFunc))
}
} catch (e) {
if (typeof returnFileFunc == 'function') {
returnFileFunc(e, "");
} else {
throw e;
}
}
});
},
/** POST function which takes the route and a return value OR Callback function. There is not version of POST which can return a file, as the client needs to handle it by making a getfile call.**/
post: function(onPath, returnFunction, options) {
app.post(onPath, getContentType(options), function(req, res) {
try {
if (typeof returnFunction == 'function') {
res.status(200).send(returnFunction(false, req.body));
} else {
res.status(200).send(JSON.stringify(returnFunction));
}
} catch (e) {
if (typeof returnFunction == 'function') {
res.status(400).send(returnFunction(e, ""));
} else {
throw e;
}
}
});
},
/** MultiPart POST function with two options for either reading fields or files.**/
multipost: function(onPath, returnFunction, options) {
app.use(onPath, multer({
dest: options.FilePath,
rename: options.Rename
}));
app.post(onPath, function(req, res) {
try {
if (typeof returnFunction == 'function') {
if (options.PostType == 'field') {
res.status(200).send(returnFunction(false, req.body));
} else if (options.PostType == 'file') {
res.status(200).send(returnFunction(false, req.files));
}
} else {
res.status(200).send(JSON.stringify(returnFunction));
}
} catch (e) {
if (typeof returnFunction == 'function') {
res.status(400).send(returnFunction(e, ""));
} else {
throw e;
}
}
});
},
put: function(onPath, returnFunction) {
app.put(onPath, function(req, res) {
try {
if (typeof returnFunction == 'function') {
res.status(200).send(returnFunction(false, req.params));
} else {
res.status(200).send(JSON.stringify(returnFunction));
}
} catch (e) {
if (typeof returnFunction == 'function') {
res.status(400).send(returnFunction(e, ""));
} else {
throw e;
}
}
});
},
delete: function(onPath, returnFunction) {
app.delete(onPath, function(req, res) {
try {
if (typeof returnFunction == 'function') {
res.status(200).send(returnFunction(false, req.params));
} else {
res.status(200).send(JSON.stringify(returnFunction));
}
} catch (e) {
if (typeof returnFunction == 'function') {
res.status(400).send(returnFunction(e, ""));
} else {
throw e;
}
}
});
},
setHeaders: function(headerObj) {
var headers = Object.keys(headerObj);
app.use(function(req, res, next) {
for (i in headers) {
res.header(headers[i], headerObj[headers[i]]);
}
next();
});
},
getSocketServer: function() {
server = require('http').Server(app);
var io = require('socket.io')(server);
return io;
},
/**
listen function for http
**/
listen: function(port, retFunction) {
if (port < 1024) {
throw new Error('Port needs to be greater than 1024');
}
this.port = port;
var curServ = server || http.createServer(app) ;
curServ.listen(port, retFunction);
},
/**
listen function for https
**/
listens: function(port, options, retFunction) {
if (port < 1024) {
throw new Error('Port needs to be greater than 1024');
}
this.ports = port;
https.createServer(options, app).listen(port, retFunction);
},
port: rport,
ports: rports
};
//************************************************************************************************