-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (58 loc) · 1.52 KB
/
app.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
"use strict";
function loadOptionalModule(module, listModules) {
try {
var loadedModule = require("./lib/" + module);
if(!!listModules && !!loadedModule) {
listModules.push(loadedModule);
}
return loadedModule;
} catch(e) {
console.log("An error occurs when loading module " + module + ".", e);
}
}
var express = require("express");
var app = express();
var http = require("http").Server(app);
var path = require("path");
var io = require("socket.io")(http);
var triggers = [];
loadOptionalModule("keyboard-trigger", triggers);
loadOptionalModule("gpio-trigger", triggers);
// Handlers
app.all("*", function(req, res, next) {
// console.log(req);
next();
});
// API
app.get("/api/:action/*", function(req, res){
io.emit(req.params.action, req.params[0]);
res.send();
});
// Public pages
var wwwOptions = { root: __dirname + "/www/" };
app.get("/", function(req, res){
res.sendFile("index.html", wwwOptions);
});
app.get("/static/:file", function(req, res) {
res.sendFile(req.params.file, wwwOptions);
});
// Resources
app.use(express.static(path.join(__dirname, "data")));
http.listen(3000, function(){
console.log("listening on *:3000");
});
io.on("connection", function(socket){
console.log("webpage connected");
triggers.forEach(function(trigger) {
trigger.on("trigger", function(id) {
console.log("trigger", id);
socket.emit("play", id);
});
});
socket.on("ready", function(msg){
console.log("webpage ready");
});
socket.on("disconnect", function(){
console.log("webpage disconnected");
});
});