-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (59 loc) · 1.86 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const path = require('path');
const methodOverride = require('method-override');
const passport = require('passport');
const session = require('express-session');
const bcrypt = require('bcrypt');
const flash = require('express-flash');
const striptags = require('striptags');
const moment = require('moment');
const truncate = require('truncate');
const MongoStore = require('connect-mongo');
const PORT = process.env.PORT || 80;
//Load configuration
dotenv.config({path:'./config/config.env'});
require('./config/db');
//Passport configuration
require('./config/passport')(passport);
//intializing express and session and passport
const app = express();
app.use(session({
secret:process.env.SESSION_SECRET,
resave:false,
saveUninitialized:true,
store: MongoStore.create({mongoUrl: process.env.MONGODB_URI})
}));
app.use(express.urlencoded({extended:false}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
//static
app.use(express.static(path.join(__dirname, 'public')));
//Pug
app.set('view engine', 'pug');
//Method-Override
app.use(methodOverride(function(req, res) {
if(req.body && typeof req.body === 'object' && '_method' in req.body) {
var method = req.body._method;
delete req.body._method;
return method;
}
}));
//Currently logged in
app.use((req, res, next) => {
res.locals.user = req.user || null;
next();
});
//Routes
app.use('/', require('./routes/index'));
app.use('/auth', require('./routes/auth'));
app.use('/login', require('./routes/login'));
app.use('/dashboard/your-stories', require('./routes/dashboard/story'));
app.get('*', (req,res) => {
res.status(404).render('error/404');
});
app.listen(PORT, (req,res) => {
console.log(`Server is running on port ${PORT}`)
});