3d-interface/server.js

67 lines
1.6 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');
// pg conf
var pgc = require('./private');
2015-05-05 11:56:35 +02:00
var app = express();
var urls = require('./urls');
2015-05-05 16:30:51 +02:00
app.set('view engine', 'jade');
2015-05-05 11:56:35 +02:00
app.use(function(req, res, next) {
res.locals.title = "3DUI";
res.locals.urls = urls;
next();
});
// Load controllers
require('./lib/boot')(app, { verbose: !module.parent });
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-06 16:51:39 +02:00
console.log("Starting server on " + server_ip_address + ":" + server_port);
app.listen(server_port, server_ip_address);
2015-05-18 10:48:37 +02:00
console.log("Trying to connect to the db...");
pg.connect(pgc.url, function(err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
client.query('INSERT INTO Users(name) VALUES(\'Hello\')');
});