-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
43 lines (34 loc) · 1.1 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
// dependencies
const express = require('express');
const routes = require('./routes/index');
const bodyParser = require('body-parser');
const passport = require('passport');
const mongoose = require('mongoose');
const User = require('./models/user');
const app = express();
// use static authenticate method of model in LocalStrategy
passport.use(User.createStrategy());
// use static serialize and deserialize of model for passport session support
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// parse json
app.use(bodyParser.json());
// use sessions
app.use(require('express-session')({
secret: 'secret123',
resave: false,
saveUninitialized: false
}));
// Connect Passport to express
app.use(passport.initialize());
app.use(passport.session());
// mongoose
mongoose.connect('mongodb://localhost/express-mongo-passport', (err) => {
if (err) {
console.log('Error connecting to database', err);
} else {
console.log('Connected to database!');
}
});
app.use('/', routes);
app.listen(3000, () => console.log('Listening on http://localhost:3000'));