-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (46 loc) · 1.9 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
// server.js
// set up ======================================================================
// get all the tools we need
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
let port = process.env.PORT;
if (port == null || port == "") {
port = 8080;
}
const mongoose = require('mongoose');
const passport = require('passport');
const flash = require('connect-flash');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const configDB = require('./config/database.js');
const setupRoutes = require('./app/routes.js')
let db
// configuration ===============================================================
mongoose.connect(configDB.url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, database) => {
if (err) return console.log(err)
db = database
// require('./app/routes.js')(app, passport, db)
setupRoutes(app, passport, db);
}); // connect to our database
require('./config/passport')(passport); // pass passport for configuration
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(express.json({ extended: true })); // get information from html forms
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'))
app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(session({
secret: 'rcbootcamp2019a', // session secret
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);