This repository has been archived by the owner on May 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
104 lines (93 loc) · 3.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ==== Globals ==== */
Config = require('config'); // global config object that stores all the configurations for diff parts of the app
/* ==== Node.js Packages to use ==== */
var express = require('express'); // For a full functioning web server
var http = require('http'); // Need this to create the express server
var path = require('path'); // For putting the right separators between file folders
var helmet = require('helmet'); // For higher level security
/* ======================================================== */
/* ==== Make jade templates available in browsers via javascript template functions ==== */
var templatizer = require('templatizer');
templatizer(path.join(__dirname,'views'), path.join(__dirname,'public/js/build/templates.js'));
/* ======================================================== */
/* ==== Setup Express ==== */
var app = express(); // create an instance of our express app
var OrientDBStore = require('connect-orientdb')(express); // to handle sessions
/* ==== Configure persistent session settings */
var settings = {
server: {
host: Config.DB.host,
port: Config.DB.port
},
db: {
user_name: Config.DB.username,
user_password: Config.DB.password
},
database: Config.DB.name,
class_name: "Session",
reap_interval: 30*60*1000
};
var sessionStore = new OrientDBStore(settings); // create a session storage in OrientDB
/* ========================================================= */
/* ==== Configure Express ==== */
app.configure(function(){
app.set('port', process.env.PORT || Config.PROCESS.port);
app.set('views', __dirname + '/views'); // folder for templating engine to look in
app.set('view engine', 'jade'); // templating engine
app.use(require('stylus').middleware({ src: __dirname + '/public' })); // styling engine and folder
app.use(express.favicon()); // auto generate a favicon
app.use(express.logger('dev')); // log everything that happens in the server
app.use(express.bodyParser());
// helmet for increased security
app.use(express.methodOverride());
app.use(helmet.xframe());
app.use(helmet.iexss());
app.use(helmet.contentTypeOptions());
app.use(helmet.cacheControl());
// Session & Cookies
app.use(express.cookieParser());
app.use(express.session({
secret: "187n mnko21908qnxxgy1n9x1",
cookie: {httpOnly: true, maxAge: 10*60*1000},
store: sessionStore
}));
// Implement Express' built in CSRF
app.use(express.csrf());
app.use(function (req, res, next) {
// make sure we pass a token to our jade templates. These tokens could be embedded in HTML forms.
res.locals.csrftoken = req.session._csrf;
next();
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// error handling for development envt
app.configure('development', function(){
app.use(express.errorHandler());
});
// error handling for production envt
app.configure('production', function(){
// custom error handler
app.use(function(err, req, res, next){
// passing the FB package here since our error template has still the option to login via facebook
var FB = require('fb');
var loginUrl = FB.getLoginUrl({ scope: 'user_about_me, email, publish_stream' });
var user = req.session.user;
console.log(err);
res.render('error', {user: undefined, loginUrl: loginUrl});
});
});
/*============================================== */
/* ==== Routes ==== */
require('./router')(app);
/* =================*/
/* ==== Server ==== */
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
// drop permissions, not available on windows
if(process.env.NODE_ENV == 'production'){
console.log('production settings loaded');
process.setgid(Config.PROCESS.gid);
process.setuid(Config.PROCESS.uid);
}
});