-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (83 loc) · 2.63 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
112
113
if (process.env.NODE_ENV !== "production") {
require('dotenv').config();
}
const express = require('express')
const engine = require('ejs-mate')
const methodOverride = require('method-override')
const mongoose = require('mongoose')
const path = require('path')
const Bid = require('./models/bid')
const { resourceLimits } = require('worker_threads')
const bid = require('./models/bid')
const appError = require('./utils/appError')
const {formValidation} = require('./joiValidation/validationSchema')
const session = require('express-session')
const flash = require('express-flash')
const {bidsRouter} = require('./routes/bids')
const passport = require('passport')
const localStatergy = require('passport-local')
const User = require('./models/Users')
const {userRouter} = require('./routes/users')
const {cardsRouter} = require('./routes/cards')
const mongoSanitize = require('express-mongo-sanitize')
const app = express();
//methodoverride
app.use(methodOverride('_method'))
// connection to mongoose
mongoose.connect(process.env.MONGO).then(() => {
console.log('connected to the mongodb')
}).catch(() => {
console.log('cannot open the connection to mongoose for some reason')
})
//session configuration
app.use(
require("express-session")({
secret: "I like the pyramids.",
resave: false,
saveUninitialized: false
})
);
//flash configuration
app.use(flash())
//mongo sanitize
app.use(mongoSanitize());
//passport configuration
passport.use(new localStatergy(User.authenticate()))
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
//setting up res.locals
app.use((req,res,next) =>{
res.locals.success = req.flash('success')
res.locals.error = req.flash('error')
res.locals.isUser = req.user
next()
})
//setting static file
app.use(express.static(__dirname + "/public/"));
//url parser
app.use(express.urlencoded({extended: true}))
// views directory
app.engine('ejs', engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs')
// setting up routes
app.use('/bids',bidsRouter)
app.use('/user', userRouter)
app.use('/cards', cardsRouter)
//listen to incoming connection
app.listen(process.env.PORT || 3000, () => {
console.log("started to listen on the port 3000")
})
app.get('/', (req,res) => {
res.render('home')
})
app.get('*', (req,res) => {
res.render('notfound')
})
app.use((err,req,res,next)=> {
const { statusCode = 500 } = err;
if (!err.message) err.message = 'Oh No, Something Went Wrong!'
res.status(statusCode).render('notfound', { err })
})