-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (44 loc) · 1.41 KB
/
index.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
const path = require("path");
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const { checkForAuthenticationCookie } = require("./middleware/authentication");
const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: true }));
const userRoute = require("./routes/user");
const blogRoute = require("./routes/blog");
const Blog = require("./models/blog");
// Middleware Plugin
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(checkForAuthenticationCookie("token"));
// Middleware for allowing Express to use 'public' folder as static
app.use(express.static(path.resolve("./public")));
// Enviornment Setup for EJS
app.set("view engine", "ejs");
app.set("views", path.resolve("./views"));
// Connection
mongoose.connect("mongodb://localhost:27017/blog-app")
.then(() => {
console.log("MongoDB Connected.")
})
.catch((error) => {
console.log("MongoDB Error.")
});
app.get("/", async (req, res) => {
const allBlogs = await Blog.find({});
res.render("home", {
user: req.user,
blogs: allBlogs,
});
});
// Routes
app.use("/user", userRoute);
app.use("/blog", blogRoute);
// Server Listening
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Server is Listening at PORT:${PORT}.`);
});