3d-interface/server.js

90 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-05-05 11:56:35 +02:00
var http = require('http');
var express = require('express');
2015-05-05 16:30:51 +02:00
var jade = require('jade');
2015-05-18 10:48:37 +02:00
var pg = require('pg');
2015-05-18 17:51:20 +02:00
// secret variables
var secret = require('./private');
2015-05-05 11:56:35 +02:00
var app = express();
2015-05-18 17:51:20 +02:00
var bodyParser = require('body-parser');
var session = require('cookie-session');
2015-05-18 17:51:20 +02:00
var cookieParser = require('cookie-parser');
2015-05-05 11:56:35 +02:00
var urls = require('./urls');
2015-05-05 16:30:51 +02:00
app.set('view engine', 'jade');
app.set('trust proxy', 1);
2015-05-05 11:56:35 +02:00
app.use(cookieParser(secret.secret));
2015-05-18 17:51:20 +02:00
app.use(session({
2015-05-19 11:56:47 +02:00
keys: [secret.secret]
2015-05-18 17:51:20 +02:00
}));
2015-05-18 15:33:14 +02:00
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
2015-05-05 11:56:35 +02:00
app.use(function(req, res, next) {
res.locals.title = "3DUI";
res.locals.urls = urls;
next();
});
2015-05-19 14:25:35 +02:00
// Set a cookie to know if already came. If not, France laws force to
// warn the user that the website uses cookies.
app.use(function(req, res, next) {
if (req.cookies.alreadyCame) {
res.locals.alertCookie = false;
} else {
res.locals.alertCookie = true;
2015-05-19 11:12:38 +02:00
res.cookie('alreadyCame', true, {maxAge: 604800000}); // One week in ms
}
next();
});
2015-05-05 11:56:35 +02:00
// Load controllers
2015-05-19 11:12:38 +02:00
console.log("Loading controllers :");
require('./lib/controllers')(app, { verbose: !module.parent });
// Load post to log data from user study
console.log("Loading posts :");
require('./lib/posts')(app, { verbose: !module.parent });
2015-05-05 11:56:35 +02:00
2015-05-19 14:25:35 +02:00
// Static files
2015-05-05 11:56:35 +02:00
app.use('/static', express.static('static'));
2015-05-06 16:32:00 +02:00
// When error raised
2015-05-05 11:56:35 +02:00
app.use(function(err, req, res, next) {
if (err.status === 404) {
res.setHeader('Content-Type', 'text/html');
2015-05-05 16:30:51 +02:00
res.render('404.jade', res.locals, function(err, result) {
2015-05-05 11:56:35 +02:00
res.send(result);
});
}
});
2015-05-06 16:32:00 +02:00
// When route not found, raise not found
2015-05-05 11:56:35 +02:00
app.use(function(req, res) {
res.setHeader('Content-Type', 'text/html');
2015-05-05 16:30:51 +02:00
res.render('404.jade', res.locals, function(err, result) {
2015-05-05 11:56:35 +02:00
res.send(result);
});
});
2015-05-06 16:52:27 +02:00
// Set ports and ip address
2015-05-06 16:51:39 +02:00
var server_port, server_ip_address;
2015-05-06 16:32:00 +02:00
if ( app.get('env') === 'development' ) {
server_port = 4000;
2015-05-06 16:51:39 +02:00
server_ip_address = 'localhost';
2015-05-06 16:32:00 +02:00
} else {
2015-05-06 16:51:39 +02:00
// Openhift conf
2015-05-06 16:32:00 +02:00
server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
}
2015-05-19 14:25:35 +02:00
// Start server
console.log("Server ready : now listening " + server_ip_address + ":" + server_port);
2015-05-06 16:51:39 +02:00
app.listen(server_port, server_ip_address);