This repository has been archived by the owner on Mar 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (46 loc) · 1.6 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
// importing packages
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const Student = require('./models/studentModel');
const index = require('./routes/index');
// connect to database
const mongoURI = process.env.MONGOURI || 'mongodb://olinjs:[email protected]:11441/olin-course-planner';
mongoose.connect(mongoURI);
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
const app = express();
app.set('port', (process.env.PORT || 3000));
// view engine setup, middleware
app.use(express.static(path.join(__dirname, '/public')));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
}));
// authentication with passport LocalStrategy and express-session
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
}, Student.authenticate()));
app.use(session({
secret: '1568189581',
resave: false,
saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((userId, done) => {
Student.findById(userId, (err, user) => { done(null, user); });
});
app.use('/', index);
app.listen(app.get('port'), () => {
console.log('Server started: http://localhost:' + app.get('port') + '/');
});