-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (50 loc) · 1.65 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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const express = require('express');
const methodOverride = require('method-override');
const ejsMate = require('ejs-mate');
const session = require('express-session');
const mongoSanitize = require('express-mongo-sanitize');
const userRoutes = require('./routes/users');
const campgroundRoutes = require('./routes/campgrounds');
const reviewRoutes = require('./routes/reviews');
// Database
const { db } = require('./configs/db_config');
db.on('error', console.error.bind(console, 'Connection Error:'));
db.once('open', () => {
console.log('DATABASE CONNECTED!!');
});
// Initialise App
app = express();
// Configs For Our App
app.use(methodOverride('_method'));
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(mongoSanitize());
app.locals.moment = require('moment');
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('ejs', ejsMate);
// Setting Up Session
const { sessionConfig } = require('./configs/session_config');
app.use(session(sessionConfig));
// Setting Up Passport
require('./helpers/passport')(app);
// Setting Up flash
require('./helpers/flash')(app);
// Setting Helmet
require('./helpers/helmet')(app);
// Route Handlers
app.get('/', (req, res) => res.render('home'));
app.use('/campgrounds', campgroundRoutes);
app.use('/campgrounds/:id', reviewRoutes);
app.use('/', userRoutes);
// Error handlers
require('./routes/errors')(app);
// Starting Up Server
const port = process.env.PORT || 3000;
app.listen(port, (req, res) => {
console.log(`LISTENING ON PORT ${port}!`);
});