-
Notifications
You must be signed in to change notification settings - Fork 37
/
app.js
85 lines (64 loc) · 2.68 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
const express = require("express");
const app = express();
var mongoose = require("mongoose");
var passport = require("passport");
var auth = require("./controllers/auth");
var store = require("./controllers/store");
var User = require("./models/user");
var localStrategy = require("passport-local");
//importing the middleware object to use its functions
var middleware = require("./middleware"); //no need of writing index.js as directory always calls index.js by default
var port = process.env.PORT || 3000;
app.use(express.static("public"));
/* CONFIGURE WITH PASSPORT */
app.use(
require("express-session")({
secret: "decryptionkey", //This is the secret used to sign the session ID cookie.
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize()); //middleware that initialises Passport.
app.use(passport.session());
passport.use(new localStrategy(User.authenticate())); //used to authenticate User model with passport
passport.serializeUser(User.serializeUser()); //used to serialize the user for the session
passport.deserializeUser(User.deserializeUser()); // used to deserialize the user
app.use(express.urlencoded({ extended: true })); //parses incoming url encoded data from forms to json objects
app.set("view engine", "ejs");
//THIS MIDDLEWARE ALLOWS US TO ACCESS THE LOGGED IN USER AS currentUser in all views
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
next();
});
/* TODO: CONNECT MONGOOSE WITH OUR MONGO DB */
app.get("/", (req, res) => {
res.render("index", { title: "Library" });
});
/*-----------------Store ROUTES
TODO: Your task is to complete below controllers in controllers/store.js
If you need to add any new route add it here and define its controller
controllers folder.
*/
app.get("/books", store.getAllBooks);
app.get("/book/:id", store.getBook);
app.get("/books/loaned",
//TODO: call a function from middleware object to check if logged in (use the middleware object imported)
store.getLoanedBooks);
app.post("/books/issue",
//TODO: call a function from middleware object to check if logged in (use the middleware object imported)
store.issueBook);
app.post("/books/search-book", store.searchBooks);
/* TODO: WRITE VIEW TO RETURN AN ISSUED BOOK YOURSELF */
/*-----------------AUTH ROUTES
TODO: Your task is to complete below controllers in controllers/auth.js
If you need to add any new route add it here and define its controller
controllers folder.
*/
app.get("/login", auth.getLogin);
app.post("/login", auth.postLogin);
app.get("/register", auth.getRegister);
app.post("/register", auth.postRegister);
app.get("/logout", auth.logout);
app.listen(port, () => {
console.log(`App listening on port ${port}!`);
});