This repository was archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (35 loc) · 1.22 KB
/
server.js
File metadata and controls
40 lines (35 loc) · 1.22 KB
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
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var static = require('express-static');
// Listen on port 8000
// Uses process.env.PORT for Heroku deployment as Heroku will dynamically assign a port
server.listen(process.env.PORT || 8000);
// Serve static files
app.use("/css", static(__dirname + '/css'));
app.use("/js", static(__dirname + '/js'));
// Index route
app.get('/', function (req, res) {
// Basic user agent check - tests for mobile
var ua = req.headers['user-agent'];
if (/mobile/i.test(ua)) {
// Send mobile to the navigation controls
res.sendFile(__dirname + '/pages/mobile.html');
} else {
// Send desktop to the main site
res.sendFile(__dirname + '/pages/index.html');
}
});
// Dynamic page route
app.get('/page/:id', function( req, res ) {
res.sendFile(__dirname + '/pages/' + req.params.id + '.html');
});
// Socket IO
io.on('connection', function (socket) {
// Create a room to broadcast to
socket.join('main');
socket.on('statechange', function (data) {
// Broadcast changes to all clients in room
socket.to('main').emit('urlchange', { url : data.url });
});
});