-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
188 lines (156 loc) · 4.34 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
var express = require('express')
var app = express();
var bodyParser = require('body-parser');
var os = require('os');
// get an instance of router
var removeRouter = express.Router();
var busCountJson = {
"bus-stop-111111":
{
"bus-05":1,
"bus-12":1,
"bus-24":1
},
"bus-stop-222222":
{
"bus-05":1,
"bus-12":1,
"bus-24":1
}
};
// instruct the app to use the `bodyParser()` middleware for all routes
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname));
app.use(function (req, res, next){
res.set('X-Powered-By', 'bus app');
next();
});
app.use('/remove',removeRouter);
app.post('/', function(req, res){
res.redirect('/thankyou.html');
reqBodyData = req.body;
if(Object.keys(reqBodyData).length == 0)
console.log("received an empty request!");
else {
var busList = Object.keys(reqBodyData);
var busStopId = reqBodyData["busStopId"];
console.log("busStopID", busStopId);
if(!busCountJson.hasOwnProperty(busStopId))
busCountJson[busStopId] = {};
var currentBusStopJson = busCountJson[busStopId];
var busStopBusJson = {};
busList.forEach(function(bus){
if(bus !== "busStopId"){
if(currentBusStopJson.hasOwnProperty(bus))
currentBusStopJson[bus] += 12;
else{
currentBusStopJson[bus] = 1;
}
}
});
console.log("new req: ",busCountJson);
}
});
//this will return us the n/w reacheable address
app.get('/pubip', function(req, res){
var response = [];
var networkInterfaces = os.networkInterfaces();
nwInterfaces = Object.keys(networkInterfaces);
//console.log(Object.keys(networkInterfaces));
for(i=0;i< nwInterfaces.length;i++){
if(nwInterfaces[i] === "wlan1"){
wlan1Info = networkInterfaces[nwInterfaces[i]];
//console.log(networkInterfaces[nwInterfaces[i]]);
for(a=0;a< wlan1Info.length;a++){
if(wlan1Info[a].family === "IPv4"){
response.push(wlan1Info[a].address);
//console.log(wlan1Info[a].address);
break;
}
}
}
}
res.json(response);
});
app.get('/summary', function(req, res){
res.json(busCountJson);
console.log("new busCount req");
});
//get a full list of bus stop
app.get('/summary/:bus', function(req, res){
//res.json(Object.keys(busCountJson));
var resJson = {};
var bus = req.params.bus;
for(busStop in busCountJson){
if(busCountJson[busStop].hasOwnProperty(bus)) {
resJson[busStop] = busCountJson[busStop][bus];
}
}
res.json(resJson);
console.log('get bus summary', resJson);
});
//get a full list of bus stop
app.get('/busstop', function(req, res){
res.json(Object.keys(busCountJson));
console.log("new bus stop req");
});
//get the bus count for a particular bus stop
app.get('/buscount/:busstop', function(req, res){
reqBusStop = req.params.busstop;
if(busCountJson.hasOwnProperty(reqBusStop)){
res.send(busCountJson[reqBusStop]);
}
else
res.send("invalid bus stop");
});
app.delete('/clearall', function(req, res){
busCountJson = {};
res.json(busCountJson);
console.log("bus data is cleared");
});
// check for the 'bus_stop' param only for /remove route
removeRouter.param('bus_stop', function(req, res, next, bus_stop) {
var reqBusStop = req.params.bus_stop;
console.log(reqBusStop);
if(!busCountJson.hasOwnProperty(reqBusStop)) {
res.send("bus stop not found");
console.log("bus stop not found");
}
else {
req.bus_stop = bus_stop;
next();
}
});
// check for the 'bus' param only for /remove route
removeRouter.param('bus', function(req, res, next, bus) {
var reqBus = req.params.bus;
if(!busCountJson[req.bus_stop].hasOwnProperty(reqBus)) {
res.send("bus not found");
console.log("bus not found");
}
else {
req.bus_id = bus;
next();
}
});
removeRouter.delete('/:bus_stop', function(req, res){
var reqBusStop = req.bus_stop;
res.send(reqBusStop + " is removed");
delete busCountJson[reqBusStop];
console.log(reqBusStop + " is removed");
});
removeRouter.delete('/:bus_stop/:bus', function(req, res){
var reqBusStop = req.bus_stop,
reqBus = req.bus;
res.send(reqBus + " is removed");
delete busCountJson[reqBusStop][reqBus];
});
app.get('*', function(req, res) {
console.log("invalid request to -",req.originalUrl);
res.status(404).json({ error: '404', description: 'invalid url'});
});
var server = app.listen(80, function () {
var host = server.address().address
var port = server.address().port
console.log('App is listening at http://%s:%s', host, port)
})