-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.js
69 lines (59 loc) · 2.02 KB
/
app2.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
var app = require('express')(),
express = require('express'),
http = require('http'),
bodyParser = require('body-parser'),
server = require('http').Server(app);
var port = process.env.PORT || 3000;
app.set('port', port);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
console.log(req.originalUrl);
next();
});
var dayslots = {};
dayslots["Yorktown"] = {};
dayslots["Tokyo"] = {};
dayslots["Haifa"] = {};
dayslots["Zurich"] = {};
dayslots["Almaden"] = {};
dayslots["Nairobi"] = {};
dayslots["Beijing"] = {};
dayslots["Sydney"] = {};
dayslots["Yorktown"]["01-01-2015"] = true;
app.get('/reservations/isAvailable/:lab/:date', function(req, res) {
res.setHeader('Content-Type', 'application/json');
if (dayslots.hasOwnProperty(req.params.lab)) {
var date = req.params.date;
if (dayslots[req.params.lab].hasOwnProperty(date)) res.end(JSON.stringify({available: false}));
else res.end(JSON.stringify({available: true}));
} else res.send(503);
});
app.post('/reservations', function(req, res) {
res.setHeader('Content-Type', 'application/json');
if (dayslots.hasOwnProperty(req.body.data.lab)) {
var date = req.body.data.date;
dayslots[req.body.data.lab][date] = true;
res.end(JSON.stringify({success: true, reservationId: req.body.data.lab+"#"+date}));
} else res.send(503);
});
app.delete('/reservations/:reservationId', function(req, res) {
res.setHeader('Content-Type', 'application/json');
var data = req.params.reservationId.split("#");
if (dayslots.hasOwnProperty(data[0])) {
delete dayslots[data[0]][data[1]];
res.end(JSON.stringify({success: true}));
} else res.send(503);
});
var projects = {};
app.post('/projects', function(req, res) {
res.setHeader('Content-Type', 'application/json');
projects[req.body.data.code] = req.body;
res.end(JSON.stringify({success: true}));
});
app.get('/projects', function(req, res) {
res.send(projects);
});
server.listen(port, function() {
console.log('Express server listening on port '+ port);
});