-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
81 lines (68 loc) · 2.64 KB
/
app.js
File metadata and controls
81 lines (68 loc) · 2.64 KB
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
var express = require("express"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
localStrategy = require("passport-local"),
localMongoose = require("passport-local-mongoose"),
camps = require("./models/campground"),
Comment = require("./models/comment"),
User = require("./models/users"),
seedDB = require("./seeds"),
flash = require("connect-flash"),
app = express()
// Various routes
var campgroundRoutes = require("./routes/campgrounds"),
commentRoutes = require("./routes/comments"),
authRoutes = require("./routes/authorization")
// Using body parser.
app.use(bodyParser.urlencoded({ extended: true }));
// Setting view engine as 'ejs'.
app.set("view engine", "ejs");
// Make public directory to use stylesheets and custom js files.
// __dirname corresponds to the location from where this very file
// is executing.
app.use(express.static(__dirname + "/public"));
// Use of flash messages.
app.use(flash())
// These configuration should be in the same exact order.
{
// Express Session configuration.
app.use(require("express-session")({
// secret property will be used to encode and decode the sessions.
secret: "This text will be used to hash password",
resave: false,
saveUninitialized: false
}))
// Passport mandatory configuration.
{
// Passport authentication
app.use(passport.initialize())
// Persistent login sessions
app.use(passport.session())
}
}
// Reading the session, taking the data from session and convert the encoded one.
passport.use(new localStrategy(User.authenticate()))
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
// Middleware to get the current username that is logged in and send it all the routes templates.
// 'currentUser' in 'res.locals.currentUser' acts as a global variable whose scope is through out
// the template.
app.use(function(req, res, next) {
res.locals.currentUser = req.user
res.locals.errorMessage = req.flash("error")
res.locals.successMessage = req.flash("success")
next()
})
// Using routes
app.use(campgroundRoutes)
app.use(commentRoutes)
app.use(authRoutes)
// Connect to MongoDB Server
mongoose.connect("mongodb://localhost/yelp-camp-v10");
// Calling the function from seeds.js file which will add sample campgrounds and comments.
// seedDB();
// Listen to localhost at 3000 port.
app.listen(3000, function () {
console.log("Yelp Camp App is started!!");
});