-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
236 lines (215 loc) · 8.84 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var express = require('express');
var app = express();
var path = __dirname + '/';
var router = express.Router();
var app_port;
app.use(express.static(__dirname + '/'));
router.get("/",function(req,res){
res.sendFile(path + "index.html");
});
app.use("/",router);
var remoteServer = {
host: 'localhost',
port: 3000
};
if (app.get('env') === 'development') {
// set app defaults for local
app_port = process.env.PORT || 6000;
} else {
//set app defaults for heroku
app_port = process.env.PORT;
}
var app_port;
if (app.get('env') === 'development') {
// set app defaults for local
app_port = process.env.PORT || 6000;
} else {
//set app defaults for heroku
app_port = process.env.PORT;
}
app.use(function(req, res, next){
if (req.url.indexOf('ui') > -1) {
if (req.file) {
console.log('file upload :: ' + req.file);
var filePath = req.file.path;
console.dir(req.file);
var dataObj = {
fileName: req.file.fileName,
filePath: req.file.path
};
req.body.fileName = req.file.originalname;
req.body.filePath = req.file.path;
}
if (req.method == 'POST') {
var ipStr = req.connection.remoteAddress;
req.body['ipAddress'] = ipStr.substring(ipStr.lastIndexOf(':') + 1, ipStr.length);
var data = JSON.stringify(req.body);
console.log('data ::' + data);
var pOptions = {
host: remoteServer.host,
port: remoteServer.port,
path: req.url,
method: req.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
if (req.headers && req.headers.authorization) {
pOptions.headers.Authorization = req.headers.authorization;
}
console.log("POST REQUEST :: \n" + JSON.stringify(req.body, null, 2));
sendRequest(pOptions, data, function(response) {
res.json(response);
});
} else if (req.method == 'PUT') {
var data = JSON.stringify(req.body);
var pOptions = {
host: remoteServer.host,
port: remoteServer.port,
path: req.url,
method: req.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
if (req.headers && req.headers.authorization) {
pOptions.headers.Authorization = req.headers.authorization;
}
console.log("PUT REQUEST :: \n" + JSON.stringify(pOptions, null, 2));
sendRequest(pOptions, data, function(response) {
res.json(response);
});
} else if (req.method == 'DELETE') {
var data = JSON.stringify(req.body);
var dOptions = {
host: remoteServer.host,
port: remoteServer.port,
path: req.url,
method: req.method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
if (req.headers && req.headers.authorization) {
dOptions.headers.Authorization = req.headers.authorization;
}
console.log("DELETE REQUEST :: \n" + JSON.stringify(dOptions, null, 2));
sendRequest(dOptions, data, function(response) {
res.json(response);
});
} else if (req.method == 'GET') {
var gOptions = {
host: remoteServer.host,
port: remoteServer.port,
path: req.url,
method: req.method,
headers: {
'Content-Type': 'application/json'
}
};
if (req.headers && req.headers.authorization) {
gOptions.headers.Authorization = req.headers.authorization;
}
console.log("GET REQUEST :: \n" + JSON.stringify(gOptions, null, 2));
sendGetRequest(gOptions, http, function(response) {
if (gOptions.path.indexOf('/ui/filedownload') == -1 && gOptions.path.indexOf('/ui/generateReport') == -1 && gOptions.path.indexOf('/ui/downloadTimesheet') == -1) {
res.json(response);
} else {
if (response.errors) {
res.json(response);
} else {
if (gOptions.path.indexOf('/ui/filedownload') > -1) {
var contentDisposition = response.headers['content-disposition'];
var file = contentDisposition.substr(21, 43);
var fileName = contentDisposition.substr(81);
res.setHeader('Content-disposition', contentDisposition);
res.setHeader('Content-type', response.headers['content-type']);
res.download(file, fileName);
} else if (gOptions.path.indexOf('/ui/downloadTimesheet') > -1) {
var contentDisposition = response.headers['content-disposition'];
console.log('in downloadTimesheet response :::');
var file = contentDisposition.substr(21, 43);
var fileName = contentDisposition.substr(contentDisposition.lastIndexOf('/') + 1);
console.log(fileName);
res.setHeader('Content-disposition', contentDisposition);
res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.download(file, fileName);
} else {
var contentDisposition = response.headers['content-disposition'];
if (contentDisposition) {
var file = contentDisposition.substr(21, 100);
var fileName = contentDisposition.substr(81);
res.setHeader('Content-disposition', contentDisposition);
res.setHeader('Content-type', response.headers['content-type']);
res.download(file, fileName);
} else {
res.send({status: 'Error', result: 'Invalid token'});
}
}
}
}
});
}
} else {
next();
}
});
function sendRequest(options, data, callback) {
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var response = '';
res.on('data', function (chunk) {
response += chunk;
});
res.on('error', function (error) {
callback({status: 'Error', error: error});
});
res.on('end', function() {
try {
callback(JSON.parse(response));
} catch(e) {
callback({status: 'Error', error: response});
}
});
});
req.end(data);
}
function sendGetRequest(options, httpType, callback) {
var req = httpType.get(options, function(res) {
if (options.path.indexOf('/ui/filedownload') == -1 && options.path.indexOf('/ui/generateReport') == -1 && options.path.indexOf('/ui/downloadTimesheet') == -1) {
res.setEncoding('utf8');
var response = '';
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function () {
if (response.length) {
try {
callback(JSON.parse(response));
} catch (e) {
var resObj = {status: 'Failure', errors: [response]}
callback(resObj);
}
} else {
callback({});
}
});
} else {
console.log(' file or report :::');
var response = '';
callback(res);
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
var server = app.listen(app_port, function () {
var host = server.address().address
var port = server.address().port
console.log("Monica bot listening at http://%s:%s", host, port)
});