-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
88 lines (68 loc) · 2.07 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
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var fs = require('fs');
// require delete account middleware
//var config = require('./config.js');
var forgotPassword = require('../../index.js');
function start(config) {
config = config || require('./config.js');
var app = express();
// set basedir so views can properly extend layout.jade
app.locals.basedir = __dirname + '/views'; // comment out and error returns
// all environments
app.set('port', process.env.PORT || config.port || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// make JSON output simpler for testing
app.set('json spaces', 0);
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.cookieSession());
if (config.csrf) {
app.use(express.csrf());
app.use(function(req, res, next) {
var token = req.csrfToken();
res.locals._csrf = token;
// save token to a cookie so we can easily access it on the client
res.cookie('csrf', token);
next();
});
}
// set a dummy session for testing purpose
app.use(function(req, res, next) {
req.session.username = 'john';
next();
});
// use forgot password middleware with testing options
forgotPassword(app, config);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
return app;
}
// export app for testing
if(require.main === module){
// called directly
start();
} else {
// required as a module -> from test file
module.exports = function(config) {
return start(config);
};
}