-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (51 loc) · 1.78 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
/**
* Server.js
*
*/
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var request = require('request');
var url = require('url');
var schedule = require('./models/schedule');
var port = process.env.PORT || 8080;
app.set('port', port);
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function (req, res) {
res.status(200).send('ok!');
});
/* Checks whether the table is available now or for a given timestamp
@param [timestamp of time to check availability]
*/
app.get('/available', function(req, res) {
var startTime = parseInt(req.query.starttime) || schedule.currentTime();
var response = schedule.checkAvailable(startTime);
res.json(response);
});
app.get('/schedule', function(req, res) {
var reserveLength = parseInt(req.query.reserve) || 15;
var futureTime = req.query.starttime ? schedule.futureTime(req.query.starttime) : schedule.currentTime();
if (isNaN(parseInt(futureTime))) {
res.json(futureTime);
} else {
var startTime = parseInt(futureTime) || schedule.currentTime();
var endTime = schedule.endTime(parseInt(startTime), parseInt(reserveLength));
var reservedBy = req.query.name || 'anonymous';
var response = schedule.reserveTable(reserveLength, startTime, reservedBy);
res.json(response);
}
});
app.get('/cancel', function(req, res) {
var startTime = parseInt(req.query.starttime) || schedule.currentTime();
var response = schedule.cancel(startTime);
res.json(response);
});
var server = app.listen(app.get('port'), function() {
console.log('Server started: http://localhost:' + port + '/');
});
module.exports = server;