-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-server.js
executable file
·377 lines (344 loc) · 11 KB
/
express-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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var express = require("express"),
pjson = require('./package.json'),
app = express(),
port = parseInt(process.env.PORT, 10) || pjson.config.portno,
daysToCache = 86400000 * 2; //86400000 = 1 day
app.configure(function () {
app.use(express.compress());//gZip compression
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public', { maxAge: daysToCache }));
app.use(app.router);
});
/***-----------HOLDS API ------------***/
var test_map = {
'1': { "id": 1, "firstName": "Pepper", "lastName": "Thomas" },
'2': { "id": 2, "firstName": "Vinny", "lastName": "The Poo" },
'3': { "id": 3, "firstName": "Lorem", "lastName": "Ipsum" },
'4': { "id": 4, "firstName": "David", "lastName": "And Goliath" },
'5': { "id": 5, "firstName": "Spider", "lastName": "Man" }
};
var ReferentialStatus = [
{ "Id": 1, "Code": "Abc", "Name": "Test" },
{ "Id": 2, "Code": "Abc 1", "Name": "Test1" },
{ "Id": 3, "Code": "Abc 2", "Name": "Test2" },
{ "Id": 4, "Code": "Abc3", "Name": "Test3" },
{ "Id": 5, "Code": "Abc4", "Name": "Test4" },
{ "Id": 6, "Code": "Abc5", "Name": "Test5" },
{ "Id": 7, "Code": "Abc6", "Name": "Test6" },
{ "Id": 8, "Code": "Abc7", "Name": "Test7" }
];
var HoldStatus = [
{ "Id": 1, "Code": "HLD1", "Name": "Jack Harper" },
{ "Id": 2, "Code": "HLD 2", "Name": "Peter Parker" },
{ "Id": 3, "Code": "HLD 3", "Name": "Vanilla Blue" },
{ "Id": 4, "Code": "HLD 4", "Name": "David Beckhams Hold" },
{ "Id": 5, "Code": "HLD 5", "Name": "Ryan Renolds Hold" },
{ "Id": 6, "Code": "HLD 6", "Name": "Winie Hold" },
{ "Id": 7, "Code": "HLD 7", "Name": "Jacob Winnston Hold" },
{ "Id": 8, "Code": "HLD 8", "Name": "Taxi Boat Hold" }
];
var RoleList = [
{ "Id": 1, "Code": "Abc", "Name": "Administrator" },
{ "Id": 2, "Code": "DH", "Name": "Discipline" },
{ "Id": 3, "Code": "Abc 2", "Name": "Reader" },
{ "Id": 4, "Code": "Abc 3", "Name": "Site Reader" }
];
var DisciplineList = [
{ "Id": 1, "Code": "Abc", "Name": "Research" },
{ "Id": 2, "Code": "Abc 1", "Name": "SES ME" },
{ "Id": 3, "Code": "Abc 2", "Name": "CMC" },
{ "Id": 4, "Code": "Abc 3", "Name": "Civil" }
];
var ValidateUsers = {"nkuruvilla":true,
"rmohan":false,
"msamir":true
};
var tokens = [];
var authsuccess = {"state":true,"displayName":"Nikil Kuruvilla","token":"USOIFSUOFSLFUJOF"};
var authfail = {"state": false };
var UserList = [
{ "Id": 1, "Name": "Nikil Kuruvilla","Role":"Reader","Disp":"Research & Development"},
{ "Id": 2, "Name": "Sony Abraham", "Role": "Reader" },
{ "Id": 3, "Name": "James Peter", "Role": "Admin" },
{ "Id": 4, "Name": "David Beckham","Role": "Reader","Disp":"Civil Department" },
{ "Id": 5, "Name": "Ronaldo Dave","Role": "Discipline" },
{ "Id": 6, "Name": "Peter Gram", "Role": "Reader","Disp":"SES ME" },
{ "Id": 7, "Name": "Diana Pinto","Role": "Discipline" },
{ "Id": 8, "Name": "Brian David","Role": "Admin" }
];
var lastID = 8;
/*
app.get('/api/testData', function(req, res) {
res.send(test_map);
});
app.post('/api/testData', function(req, res) {
//console.log('POST:'+req.body.firstName);
test_map.Test.push({"firstName":req.body.firstName,"lastName":req.body.lastName});
res.send('ok');
});
*/
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
});
function checkToken(req) {
if (tokens.indexOf(req.header('Authorization')) < 0)
return false;
else
return true;
}
/*Referential STATUS -API : START*****************************************************************/
//GET ALL DATA
app.get('/api/Status', function (req, res) {
if (checkToken(req)) {
res.send(ReferentialStatus);
}
else
res.send(404);
});
//Add New Item
app.post('/api/Status', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = ReferentialStatus.length + 1;
temp.Code = req.body.Code;
temp.Name = req.body.Name;
ReferentialStatus.push(temp);
res.send('100');
} else
res.send(404);
});
//Update Item
app.put('/api/Status/:Id', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = req.params.Id;
temp.Code = req.body.Code;
temp.Name = req.body.Name;
//ReferentialStatus[temp.Id] = temp;
for (item in ReferentialStatus) {
if (req.params.Id == ReferentialStatus[item].Id)
ReferentialStatus[item] = temp;
}
res.send('100');
} else
res.send(404);
});
//Delete Item
app.delete('/api/Status/:Id', function (req, res) {
if (checkToken(req)) {
//delete ReferentialStatus[req.params.id];
for (item in ReferentialStatus) {
if (req.params.Id == ReferentialStatus[item].Id)
ReferentialStatus.splice(item, 1);
}
res.send('100');
} else
res.send(404);
});
/*Referential STATUS -API : END*/
/*HOLDS API*********************************************************************************/
/*Referential STATUS -API : START*****************************************************************/
//GET ALL DATA
app.get('/api/Hold', function (req, res) {
if (checkToken(req)) {
res.send(HoldStatus);
}
else
res.send(404);
});
//Add New Item
app.post('/api/Hold', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = HoldStatus.length + 1;
temp.Code = req.body.Code;
temp.Name = req.body.Name;
HoldStatus.push(temp);
res.send('100');
} else
res.send(404);
});
//Update Item
app.put('/api/Hold/:Id', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = req.params.Id;
temp.Code = req.body.Code;
temp.Name = req.body.Name;
//ReferentialStatus[temp.Id] = temp;
for (item in HoldStatus) {
if (req.params.Id == HoldStatus[item].Id)
HoldStatus[item] = temp;
}
res.send('100');
} else
res.send(404);
});
//Delete Item
app.delete('/api/Hold/:Id', function (req, res) {
if (checkToken(req)) {
//delete ReferentialStatus[req.params.id];
for (item in HoldStatus) {
if (req.params.Id == HoldStatus[item].Id)
HoldStatus.splice(item, 1);
}
res.send('100');
} else
res.send(404);
});
/*Referential STATUS -API : END*/
/*USER MANAGEMENT API**********************************************************************/
app.get('/api/AccessLevel', function (req, res) {
if (checkToken(req)) {
res.send(RoleList);
} else
res.send(404);
});
app.get('/api/Discipline', function (req, res) {
if (checkToken(req)) {
res.send(DisciplineList);
} else
res.send(404);
});
//Add New Item
app.post('/api/Discipline', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = DisciplineList.length + 1;
temp.Code = req.body.Code;
temp.Name = req.body.Name;
DisciplineList.push(temp);
res.send('100');
} else
res.send(404);
});
//Update Item
app.put('/api/Discipline/:Id', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = req.params.Id;
temp.Code = req.body.Code;
temp.Name = req.body.Name;
//ReferentialStatus[temp.Id] = temp;
for (item in DisciplineList) {
if (req.params.Id == DisciplineList[item].Id)
DisciplineList[item] = temp;
}
res.send('100');
} else
res.send(404);
});
//Delete Item
app.delete('/api/Discipline/:Id', function (req, res) {
if (checkToken(req)) {
//delete ReferentialStatus[req.params.id];
for (item in DisciplineList) {
if (req.params.Id == DisciplineList[item].Id)
DisciplineList.splice(item, 1);
}
res.send('100');
} else
res.send(404);
});
app.post('/api/users/validateUser', function (req, res) {
if (checkToken(req)) {
res.send(ValidateUsers);
} else
res.send(404);
});
//GET ALL
app.get('/api/Users', function (req, res) {
if (checkToken(req)) {
res.send(UserList);
} else
res.send(404);
});
//Add New Item
app.post('/api/User', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = UserList.length + 1;
temp.Name = req.body.Name;
temp.UserId = req.body.UserId;
UserList.push(temp);
res.send('100');
} else
res.send(404);
});
//Update Item
app.put('/api/Users/:Id', function (req, res) {
if (checkToken(req)) {
var temp = {};
temp.Id = req.params.Id;
temp.Name = req.body.Name;
temp.UserId = req.body.UserId;
//ReferentialStatus[temp.Id] = temp;
for (item in UserList) {
if (req.params.Id == UserList[item].Id)
UserList[item] = temp;
}
res.send('100');
} else
res.send(404);
});
//Delete Item
app.delete('/api/Users/:Id', function (req, res) {
if (checkToken(req)) {
//delete ReferentialStatus[req.params.id];
for (item in UserList) {
if (req.params.Id == UserList[item].Id)
UserList.splice(item, 1);
}
res.send('100');
} else
res.send(404);
});
/*USER MANAGEMENT API : END*/
/*Authentication API**********************************************************************/
app.post('/api/auth', function (req, res) {
var user = req.body.UserID;
var pass = req.body.Password;
if (user == "cdc_test1" && pass == "Technip08")
// res.send(authsuccess);
res.send(allocateKey());
else
res.send(500);
});
app.get('/api/auth/logout', function (req, res) {
if (checkToken(req)) {
if (tokens.indexOf(req.header('Authorization')) >= 0)
tokens.splice(tokens.indexOf(req.header('Authorization')), 1);
res.send(200);
} else
res.send(404);
});
var guid = (function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function () {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
})();
function allocateKey() {
var key = guid();
tokens.push(key);
var obj = { "state": true, "displayName": "Nikil Kuruvilla", "token": key };
return obj;
}
/*Authentication API END**********************************************************************/
app.listen(port);
console.log('Now serving the app at http://localhost:' + port + '/');