-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
82 lines (76 loc) · 2.73 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
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
mongoose.Promise = Promise
//models require
const Campground = require('./models/campground')
const Comment = require('./models/comment')
//user model for auth
const User = require('./models/user')
//add seeds.js and execute seed function to destroy and create new sample data on db
const seedDB = require('./seeds')
//seedDB() //Seed the DataBase
//for user authentication
const passport = require('passport')
const flash = require('connect-flash')
const LocalStrategy = require('passport-local')
const session = require('express-session')
const methodOverride = require('method-override')
//Routes:
const campgroundRoutes = require('./routes/campgrounds')
const commentsRoutes = require('./routes/comments')
const indexRoutes = require('./routes/index')
//-------------------CONNECT TO DB--------------
// Using `mongoose.connect`...
mongoose.connect('mongodb://admin:admin@ds255797.mlab.com:55797/yelp-v10', {
useMongoClient: true
/* other options */
})
//---------------APP CONFIG-----------------
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: true }))
// official is this, but path not defined:
//app.use(express.static(path.join(__dirname, "public")))
app.use(express.static(__dirname + '/public'))
app.use(methodOverride('_method'))
//---------------PASSPORT CONFIG-----------------
app.use(
session({
secret: 'pasaporte falso',
resave: false,
saveUninitialized: false
})
)
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
//use localstrategy and give it a method (User.authenticate)from local-mongoose plugin
passport.use(new LocalStrategy(User.authenticate()))
// use static serialize and deserialize of model for passport session support
//serializeUser and deserializeUser methods taken from local-mongoose, it's easier
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
//creates middleware to pass user data (check if logged in) for EVERY route
app.use((req, res, next) => {
res.locals.currentUser = req.user
//Access flash message
res.locals.successMsg = req.flash('success')
res.locals.errorMsg = req.flash('error')
next()
})
//Use routes
app.use(indexRoutes)
app.use(campgroundRoutes)
app.use(commentsRoutes)
//-------------404 PAGE-----------------
app.get('*', (req, res) => {
res.send('404 NOTHING TO SEE HERE...')
})
//-------------APP LISTEN 3000---------------
// process.env.PORT lets the port be set by Heroku, on localhost or codeanywhere is 3000
let port = process.env.PORT || 3000
//check connection through express
app.listen(port, function() {
console.log(`Our app is running on http://localhost:${port}`)
})