slideshow.io/server.js

73 lines
1.7 KiB
JavaScript

require('app-module-path').addPath('./lib');
var http = require('http');
var express = require('express');
var pug = require('pug');
var bp = require('body-parser');
var socket = require('socket.js');
var log = require('log.js');
var app = express();
// Socket.io initialization
var http = require('http').Server(app);
var io = require('socket.io')(http);
require('./lib/socket.js')(io);
var isDev = app.get('env') === 'development';
app.set('view engine', 'pug');
app.set('trust proxy', 1);
// parse application/x-www-form-urlencoded
app.use(bp.urlencoded({ extended: false }))
// parse application/json
app.use(bp.json())
// Log request and time to answer
app.use(function(req, res, next) {
let start = Date.now();
res.on('finish', function() {
log.request(req, res, Date.now() - start);
});
res.locals.title = "slideshow.io";
next();
});
// Load controllers
require('./lib/get')(app);
// Static files
app.use('/static', express.static('static'));
// When error raised
app.use(function(err, req, res, next) {
if (err.status === 404) {
res.setHeader('Content-Type', 'text/html');
res.send('Error 404');
}
});
// When route not found, raise not found
app.use(function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.send('Error 404');
});
// Set ports and ip address
var serverPort, serverIpAddress;
if ( isDev ) {
serverPort = 4001;
serverIpAddress = 'localhost';
} else {
// Openhift conf
serverPort = process.env.OPENSHIFT_NODEJS_PORT || 8080;
serverIpAddress = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
}
// Start server
http.listen(serverPort, serverIpAddress, function() {
log.debug("Now listening " + serverIpAddress + ":" + serverPort);
});