-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (52 loc) · 1.87 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const path = require('path');
const expressHandlebars = require('express-handlebars');
const flash = require('connect-flash');
const session = require('express-session');
const mongoose = require('mongoose');
const passport = require('passport');
require( './config/passport');
//Connect to db
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/siteauthentication');
const app = express(); //Start express app
//Setting up renger engine(Handlebars)
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', expressHandlebars({ defaultLayout: 'layout' }));
app.set('view engine', 'handlebars');
//Setting up Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Setting up static path file to server
app.use(express.static(path.join(__dirname, 'public')));
// Setting up Session
app.use(session({
cookie: { maxAge: 60000 },
secret: 'dickandballs',
saveUninitialized: false,
resave: false
}));
//Setting up passport
app.use(passport.initialize());
app.use(passport.session());
//Starting flash to use flash messaging
app.use(flash());
//Creatre success and error messegin and call then by names success error
app.use((req, res, next) => {
res.locals.success_message = req.flash('success');
res.locals.error_message = req.flash('error');
res.locals.isAuthenticated = req.user ? true : false;
next(); //Call next to continue
});
//Setting op default routes
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));
// catch 404 and forward to error handler
app.use((req, res, next) => {
res.render('notFound');
});
//Setting up port and listen to requests
app.listen(8080, () => console.log('Server started listening on port 8080!'));