-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (53 loc) · 1.93 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
require('dotenv').config()
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const methodOverride = require('method-override')
const Book = require('./models/book')
const User = require('./models/user')
const flash = require('connect-flash')
const passport = require('passport')
const LocalStrategy = require('passport-local')
const bookRoutes = require("./routes/books")
const indexRoutes = require('./routes/index')
const mongod_password = process.env.MONGODPASS
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
const Database_url = "mongodb+srv://dbUserVS:" + mongod_password + "@vscluster0.vfwe5.mongodb.net/test?retryWrites=true&w=majority"
mongoose.connect(Database_url, {
}).then(() => {
console.log("connected to DB");
}).catch(err => {
console.log("Error", err.message)
})
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(__dirname + '/public'))
app.set('view engine', "ejs")
app.use(methodOverride("_method"))
app.use(require('express-session')({
secret: "This a secret message okay",
resave: false,
saveUninitialized: false,
}))
app.use(flash())
app.use(passport.initialize())
app.use(passport.session())
passport.use(new LocalStrategy(User.authenticate()))
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
app.use((req, res, next) => {
res.locals.currentUser = req.user
app.locals.moment = require('moment');
res.locals.errormessage = req.flash("error")
res.locals.successmessage = req.flash("success")
next()
})
app.use(indexRoutes)
app.use(bookRoutes)
const port = process.env.PORT || 1008
app.listen(port, () => {
console.log("Server running at http://localhost:1008")
})