-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (66 loc) · 2.94 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
"use strict"
// *****************************************************************************
// *****************************************************************************
// Requires and basics
// Externals
const expressSession = require( "express-session" )
const methodOverride = require( "method-override" )
const LocalStrategy = require( "passport-local" )
const bodyParser = require( "body-parser" )
const mongoose = require( "mongoose" )
const passport = require( "passport" )
const express = require( "express" )
const flash = require( "connect-flash" )
// Own modules
// const seedDB = require( "./seeds" )
const middleware = require( "./middleware" )
// *****************************************************************************
// *****************************************************************************
// App setup is here
const app = express()
app.set( "view engine", "ejs" );
app.use( express.static( __dirname + "/public" ) )
app.use( bodyParser.urlencoded({extended: true}) )
app.use( methodOverride( "_method" ) )
app.use( flash() )
// Seed the database
// seedDB()
// *****************************************************************************
// *****************************************************************************
// Database setup + Model imports
// Local database
//mongoose.connect( "mongodb://localhost/yelp_camp" )
// mLab database
mongoose.connect( "mongodb://yc_app:[email protected]:63715/yelpcamp" )
const User = require( "./models/user" )
// *****************************************************************************
// *****************************************************************************
// Auth setup
app.use( expressSession({
secret : "This is a really awesome app and I like cats!",
resave : false,
saveUninitialized: false
}) )
app.use( passport.initialize() )
app.use( passport.session() )
passport.use( new LocalStrategy( User.authenticate() ) )
passport.serializeUser( User.serializeUser() )
passport.deserializeUser( User.deserializeUser() )
app.use( middleware.addCurrentUser )
// *****************************************************************************
// *****************************************************************************
// Router setup
const campgroundRoutes = require( "./routes/campgrounds" )
const commentRoutes = require( "./routes/comments" )
const indexRoutes = require( "./routes/index" )
app.use( "/", indexRoutes )
app.use( "/campgrounds", campgroundRoutes )
app.use( "/campgrounds/:id/comments", commentRoutes )
// *****************************************************************************
// *****************************************************************************
// Server
// Set correct port depending on local or Heroku
app.set( "port", ( process.env.PORT || 3000 ) );
app.listen( app.get( "port" ), function () {
console.log( "The YelpCamp server has started" )
})