-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (99 loc) · 4.13 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const CG_Yelpcamp = require('./models/campground');
const methodOverride = require('method-override');
const ejsMate = require('ejs-mate');
const AppError = require('./other/AppError');
const catchAsync = require('./utils/catchAsync');
const ExpressError = require('./utils/ExpressError');
const Joi = require('joi');
const { campgroundSchema, reviewSchema } = require('./schemas');
const Review = require('./models/review');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
app.use(cookieParser('thisIsMySecret'));
app.use(session({
secret: 'thisisnotagoodsecret',
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
expires: Date.now() + (1000 * 60 * 60 * 24 * 7),
maxAge: (1000 * 60 * 60 * 24 * 7)
}
}));
/* Flash messages are stored in the session. First enable cookieParser and session middleware above. Then, use flash middleware provided by connect-flash for all routes. */
app.use(flash());
/* We use this middleware to pass the flash keys (arrays) we initialised in the req object (in our respective route handlers) to res.locals so that we can access those messages when we are rendering the HTML in our views. */
app.use((req, res, next) => {
//// BEWARE If you enable the logs below the flash msgs will not display!!
// console.log("In the flash middleware")
// console.log(req.flash('success'))
// initialise our very own res.locals variables
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
})
mongoose.connect('mongodb://127.0.0.1:27017/yelpCampDB', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Database connected');
});
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
app.use(morgan('tiny'));
// Allows layouts to be used
app.engine('ejs', ejsMate);
/* express.Router() likes to keep params separate so the upshot of this is in the reviews route file the ':id' below, won't be passed in!! Routers actually get separate params but we can actually specify an option in the reviews routes file:
const router = express.Router({ mergeParams: true });
Now all of the params from here are also merged with the params in the reviews route file. */
const campgrounds = require('./routes/campgrounds');
app.use('/campgrounds', campgrounds)
const reviews = require('./routes/reviews');
app.use('/campgrounds/:id/reviews', reviews)
/* We are telling express to serve our public directory which contains static assets so that we could have images, custom style sheets and scripts */
// app.use(express.static('public'))
app.use(express.static(path.join(__dirname, 'public')))
// http://localhost:3001/campgrounds?password=pass
// Middleware to check if a password exists in the HTML request e.g.
// app.get('/campgrounds', verify, async (req, res) => {
const verify = ((req, res, next) => {
const { password } = req.query;
if (password === 'pass') {
console.log("Access allowed");
next();
}
else
// res.send("DENIED!!")
throw new Error('Passord required!');
// throw new AppError('Hey ol boy! Password required', 401);
});
app.get('/', (req, res) => {
res.render('home');
})
// Will only run if nothing else above matches
// the * represents a 'get', a 'post', etc.
app.all('*', (req, res, next) => {
next(new ExpressError('Page not found', 404));
})
app.use((err, req, res, next) => {
console.log('**************************************');
console.log('*************** ERROR ****************');
console.log('**************************************');
const { statusCode = 500 } = err;
if (!err.message) err.message = 'Oh No, something went wrong!!';
res.status(statusCode).render('error', { err });
})
app.listen(3002, () => {
console.log('Serving on port 3002')
});