-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (61 loc) · 1.92 KB
/
server.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
// Dotenv for development environment
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
// TODO:
// Implement Authorization:
// What makes users special from guests is the ability to
// mark them as read, wishlist, etc. Add that
// Check if sessions are working correctly
// Imports
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const indexRouter = require("./routes/index");
const authorsRouter = require("./routes/authors");
const booksRouter = require("./routes/books");
const authRouter = require("./routes/auth");
const bodyParser = require("body-parser");
const passport = require("passport");
const flash = require("express-flash");
const session = require("express-session");
const methodOverride = require("method-override");
// App instance
const app = express();
// App sets and middleware
app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.set("layout", "layouts/layout");
app.use(expressLayouts);
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ limit: "10mb", extended: true }));
app.use(methodOverride("_method"));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static("public"));
app.use((req, res, next) => {
res.locals.user = req.user;
next();
});
app.use(flash());
// Database connection
const mongoose = require("mongoose");
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", (error) => console.error(error));
db.once("open", () => console.log("Connected to mongoose"));
// Main routing
app.use("/", indexRouter);
app.use("/authors", authorsRouter);
app.use("/books", booksRouter);
app.use("/", authRouter);
// App start
app.listen(process.env.PORT || 4000, () =>
console.log("Connected on port 4000")
);