-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (63 loc) · 1.54 KB
/
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
//
// Dependencies.
//
var morgan = require('morgan')
var express = require('express')
var mongoose = require('mongoose')
var passport = require('passport')
var flash = require('connect-flash')
var bodyParser = require('body-parser')
var session = require('express-session')
var cookieParser = require('cookie-parser')
//
// Configuration variables.
//
var app = express()
var port = process.env.PORT || 8080
var DB = require('./config/database')
//
// Only start the application
// if the database is ready.
//
mongoose.connection.on('connected', function (ref) {
//
// Log, parse request, and alike.
//
app.use(morgan('dev'))
app.use(cookieParser())
app.use(bodyParser.json())
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: false }))
//
// Start sessions with
// a password.
//
require('./app/auth/passport')(passport) // pass passport for configuration
app.use(session({
secret: process.env.SESSION_KEY || 'rolltimekey',
saveUninitialized: false,
resave: true
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
//
// Routes (with passport).
//
require('./app/routes.js')(app, passport)
//
// Start server.
//
app.listen(port)
console.log('Rolltime Server running on port ' + port)
})
mongoose.connection.on('error', function (err) {
console.log('Could not connect to MongoDB.')
throw err
})
//
// Attempt connection with MongoDB.
//
console.log('Attempting connection to: ' + DB.url)
mongoose.connect(DB.url)