-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
82 lines (68 loc) · 2.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
/*----Require Config----*/
require('dotenv').load();
var express = require('express');
var _ = require('lodash');
var app = module.exports = express();
var bodyParser = require('body-parser');
var stallController = require('./custom_modules/stallController');
var notifyController = require('./custom_modules/notifyController');
/*----Configure Express------*/
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/webapp'));
app.use(bodyParser.json());
/*------Start Server-------*/
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});
/*--------Routes----------*/
app.get('/webapp', function(request, response) {
response.sendfile('webapp/index.html');
});
app.get('/stalls', function(request, response) {
stallController.getAllStalls(function (statuses) {
response.json({statuses: statuses});
})
});
app.post('/stall', function(request, response) {
var requestBody = request.body;
console.log("Request Body: " + JSON.stringify(requestBody) );
var floor = requestBody.floor,
bathroom = requestBody.bathroom,
stallName = requestBody.stallName,
occupied = requestBody.occupied;
if(!_.isString(floor) || !_.isString(bathroom) ||
!_.isString(stallName) || !_.isBoolean(occupied)) {
response.status(400);
response.send("Bad data, yo");
} else {
stallController.updateStallStatus(floor, bathroom, stallName, occupied, response);
}
});
app.get('/stall', function(request, response) {
var requestBody = request.query;
var floor = requestBody.floor,
bathroom = requestBody.bathroom,
stallName = requestBody.stallName;
var callback = function(val){
response.json({ "occupied": val });
};
if(!_.isString(floor) || !_.isString(bathroom) || !_.isString(stallName)) {
response.status(400);
response.send("Bad data, yo");
} else {
stallController.getStallStatus(floor, bathroom, stallName, callback);
}
});
app.post('/notify', function(request, response) {
var requestBody = request.body;
console.log("Request Body: " + JSON.stringify(requestBody) );
var floor = requestBody.floor,
bathroom = requestBody.bathroom,
email = requestBody.email;
if(!_.isString(floor) || !_.isString(bathroom) || !_.isString(email)) {
response.status(400);
response.send("Bad data, yo");
} else {
notifyController.register(floor, bathroom, email, response);
}
});