-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (76 loc) · 2.24 KB
/
index.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
var path = require('path');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.set('port', (process.env.PORT || 3000));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));
var lunch = require('./lib/lunch');
var util = require('./lib/util');
function renderHtml(req, res) {
var day = util.getDay(req.params.day);
res.render('menus', {
title: 'Lounas@Pasila',
options : util.days,
day : day,
places: lunch.places()
});
}
function renderMap(req, res) {
var day = util.getDay(req.params.day);
lunch.all(day).then(function(result) {
res.render('map', {
title: 'Lounas@Pasila',
options : util.days,
day : day,
places: result || []
});
});
}
// need to setup json before day-routes so it's triggered correctly
app.get('/lunch.json', function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
lunch.all().then(function(result) {
res.send(result);
});
});
// index and day-routes
app.get('/', function(req, res) {
renderHtml(req, res);
});
app.get('/map', function(req, res) {
renderMap(req, res);
});
app.get('/:day', function(req, res) {
renderHtml(req, res);
});
io.on('connection', function(socket){
console.log('a user connected');
var places = lunch.places();
socket.emit('places', places);
places.forEach(function(place) {
lunch.getMenu(place.id)
.then(function(menu) {
socket.emit('menu', {
id : place.id,
name : place.name,
url : place.url,
menu : menu
});
})
.catch(function(err) {
console.log('Error getting menu');
socket.emit('menu', {
id : place.id,
name : place.name,
url : place.url
});
});
});
});
var server = http.listen(app.get('port'), function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});