-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (63 loc) · 2.16 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
74
75
76
77
78
79
80
const express = require('express');
const dotenv = require('dotenv');
const app = express();
const path = require('path');
const moment = require('moment');
const favicon = require('serve-favicon');
const ejsMate = require('ejs-mate');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const passport = require('passport');
const connectDB = require('./config/db');
const ApplicationError = require('./utils/ApplicationError');
const methodOverride = require('method-override');
const mongoSanitize = require('express-mongo-sanitize');
//load configurations
dotenv.config();
require('./config/passport')(passport);
connectDB();
app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
//session middleware
const secret = process.env.SECRET || 'keyboard cat';
app.use(session({
secret: secret,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: 'mongodb://localhost:27017/storyBooks' })
}))
//passport middleware (session is used for passport deserialization)
app.use(passport.initialize());
app.use(passport.session());
//prevent nosql injection
app.use(mongoSanitize());
//method override for post and delete
app.use(methodOverride('_method'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'images/favicon.svg')));
//get the user for every route
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.moment = moment;
next();
})
//routes
app.use('/stories', require('./routes/story'));
app.use('/', require('./routes/index'));
app.use('/auth', require('./routes/auth'));
app.use('/stories/:id/comments', require('./routes/comment'));
//ROUTE NOT FOUND
app.all('*', (req, res, next) => {
next(new ApplicationError('Page not Found', 404))
})
// error handler
app.use((err, req, res, next) => {
const {statusCode = 500} = err;
if(!err.message) err.message = 'Oh No, Something Went Wrong!'
res.status(statusCode).render('error', {err});
})
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
})