-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (39 loc) · 1.57 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
var morgan = require('morgan');
var fs = require('fs');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
// Set the views directory
app.set('views', __dirname + '/views');
// Define the view (templating) engine
app.set('view engine', 'ejs');
// Define how to log events
app.use(morgan('tiny'));
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// Load all routes in the routes directory
fs.readdirSync('./routes').forEach(function (file){
// There might be non-js files in the directory that should not be loaded
if (path.extname(file) == '.js') {
require('./routes/'+ file).init(app);
}
});
// Handle static files
app.use(express.static(__dirname + '/public'));
// Catch any routes not already handed with an error message
app.use(function(req, res) {
var message = 'Error, did not understand path '+req.path;
// Set the status to 404 not found, and render a message to the user.
res.status(404).render('error', { 'message': message });
});
// Boilerplate for setting up socket.io alongside Express.
var httpServer = require('http').createServer(app);
var sio =require('socket.io')(httpServer);
// The server socket.io code is in the socketio directory.
require('./socketio/play.js').init(sio);
ipaddress = process.env.OPENSHIFT_NODEJS_IP || undefined;
port = process.env.OPENSHIFT_NODEJS_PORT || 1111;
httpServer.listen(port, ipaddress, function() {console.log('Listening on ' + port);});