-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (45 loc) · 2.75 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
/******************************************************************************
* Edison GPIO Example
******************************************************************************
* This example app uses a RESTful API and WebSockets to communicate the state
* of the hardware to the user. It was built to be a demo for the Edison
* Ethernet board (https://github.com/LGSInnovations/Edison-Ethernet).
*
* This app structure modeled afer https://github.com/plusk01/mean-skeleton.
*
**/
// ----------------------------------------------------------------------------
// Requires
// ----------------------------------------------------------------------------
var express = require('express'); // Easy API routing
var app = express(); // Create the app
var bodyParser = require('body-parser'); // Parses POST JSON automagically
var socketio = require('socket.io');
var morgan = require('morgan'); // Logging for dev
var path = require('path'); // filesystem goodies
var api = require('./app/api'); // API routes
var hwHandler = require('./app/hwHandler'); // Hardware stuff
var socket = require('./app/socket'); // socket
var port = process.env.PORT || 8080; // If no env var set, DEV mode
// ----------------------------------------------------------------------------
// Configuration
// ----------------------------------------------------------------------------
global.__base = __dirname + '/'; // so child modules can access root
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(morgan('dev')); // For request logging
hwHandler.initStates();
// ----------------------------------------------------------------------------
// Routes
// ----------------------------------------------------------------------------
app.use(express.static(path.join(__dirname, 'public'))); // for the HTML5/JS app
app.use('/api', api); // all API requests will be http://host.com/api/...
// ----------------------------------------------------------------------------
// Listen (start app: `node app.js`)
// ----------------------------------------------------------------------------
var io = socketio.listen(app.listen(port));
console.log('Server started on port ' + port);
// ----------------------------------------------------------------------------
// Socket.io Event Handler
// ----------------------------------------------------------------------------
socket.handle(io);