-
Notifications
You must be signed in to change notification settings - Fork 0
/
mentorleaf_server.js
95 lines (70 loc) · 3.02 KB
/
mentorleaf_server.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
// Express webserver for the application
//Main webserver
var express = require('express');
var app = express();
var port = process.env.PORT || 8055;
var passport = require('passport');
var mongoose = require('mongoose'); //Used to provide the structured data model
var bodyParser = require('body-parser'); //get info from html forms
var cookieParser = require('cookie-parser'); // read cookies (needed for auth)
var session = require('express-session');
var morgan = require('morgan');
var mongoStore = require('connect-mongo')({session: session});
var flash = require('connect-flash');
//Used for profile pic upload
var multer = require('multer');
var uploader = multer({ dest: './uploads/'}).single('avatar');
//Load Server Configurations for DEV testing
var dev_nconf = require('nconf');
if(process.env.PORT){
require('./config/passport')(passport); // pass passport for configuration
}else{
console.log("-LOAD DEV SERVER CONFIGS-");
//load configuration from designated file.
dev_nconf.file({ file: './config/dev_config.json' });
require('./config/passport')(passport, dev_nconf); // pass passport and dev_nconf for configuration
}
//Ensure that User model is registered in mongoose
require('./models/users_model.js');
var configDB = require('./config/database.js')(dev_nconf);
mongoose.connect(configDB.mongoUri); //Connect to the database
//Controller for operations on user accounts
var usersController = require('./controllers/users_controller.js')(dev_nconf);
// set up our express application
app.use(morgan('dev')); // log every request to the console
//EJS is used to render HTML templates
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use('/static', express.static('./static'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
// required for passport
// Uses the connect-mongo library to register the mongodb connection as the persistence
//for the authenticated sessions.
//This code adds a session property to the request object. The sessions object is directly tied
//to the sessions colelction in MongoDB so that when you make changes to the session, they are saved in the database
//maxAge: 1hr Rolling.
var configAuth = require('./config/auth')(dev_nconf);
app.use( session({
secret: configAuth.sessionAuth.secret,
cookie: {maxAge: 60 * 60 * 1000},
rolling: true,
store: new mongoStore({
mongooseConnection: mongoose.connection,
collection: 'sessions'
}),
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash()); // use connect-flash for flash messages stored in session
// Routes =================================================================================
// load routes and pass in app and fully configured passport
require('./routes')(app, passport, usersController, uploader);
// Launch =================================================================================
app.listen(port);