-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (99 loc) · 4.12 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
114
115
116
//======================================================================================================================================
// SETUP
//======================================================================================================================================
// INCLUDE MODULES
//=============================================
var express = require("express"),
expressSession = require('express-session'),
methodOverride = require('method-override'),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
seedDB = require("./seeds"),
passport = require('passport'),
LocalStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose'),
http = require('http');
flash = require('connect-flash');
// Wipe database and refill it with new seed data
// seedDB();
// DATABASE SETUP
//=============================================
// Connect mongoose to mongodb
// Local
//mongoose.connect("mongodb://localhost:27017/Yelp_Camp", { useNewUrlParser: true });
// MongoDB Atlas
console.log(process.env.DATABASEURL);
mongoose.connect(process.env.DATABASEURL, { useNewUrlParser: true }, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + process.env.DATABASEURL + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + process.env.DATABASEURL);
}
});
// Include mongoose models
var Campground = require("./models/campground"),
User = require("./models/user"),
Comment = require("./models/comment");
// APP SETUP
//=============================================
// Define app
app = express();
// Use method-override
app.use(methodOverride('_method'));
// Use connect-flash
app.use(flash());
// Use body parser
app.use(bodyParser.urlencoded({extended: true}));
// View files as ".ejs"
app.set('view engine', 'ejs');
// Serve the "public" directory
app.use(express.static("./public"));
// Use express-session
app.use(expressSession({
secret: 'Sample string to generate unique session key',
resave: false,
saveUninitialized: false
}));
// PASSPORT SETUP
//=============================================
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// STRATEGY definition(s)
// 'local'
passport.use(new LocalStrategy(User.authenticate()));
// CUSTOM MIDDLEWARES
//=============================================
// define currentUser and sotre in res.locals
app.use(function(req, res, next) {
res.locals.currentUser = req.user;
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
});
//======================================================================================================================================
// ROUTES
//======================================================================================================================================
// Require routes
var indexRoutes = require('./routes/index');
var campgroundsRoutes = require('./routes/campgrounds');
var commentsRoutes =require('./routes/comments');
// Define routes
app.use('/', indexRoutes);
app.use('/campgrounds', campgroundsRoutes);
app.use('/campgrounds/:id/comments', commentsRoutes);
//======================================================================================================================================
// Local & Web
let PORT = process.env.PORT;
let localHost = false;
if (PORT == null || PORT == "") {
PORT = 1234;
localHost = true;
}
app.listen(PORT, process.env.IP, () => {
console.log('Yelp Camp app started.');
if (localHost) {
require('open')('http://localhost:' + PORT);
}
});