-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (35 loc) · 1.04 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
import express from "express";
import dotenv from "dotenv";
import connectToDatabase from "./database/db.js";
import path from "path";
import userRouter from "./routes/user.js";
import cookieParser from "cookie-parser";
import checkForAuthenticationCookie from "./middlewares/authentication.js";
import blogRouter from "./routes/blog.js";
import Blog from "./models/blog.js";
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
connectToDatabase();
app.set("view engine", "ejs");
app.set("views", path.resolve("./views"));
app.use(express.json());
app.use(cookieParser());
app.use(express.static(path.resolve("./public")));
app.use(checkForAuthenticationCookie("token"));
app.use(express.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
try {
const allBlogs = await Blog.find({});
res.render("home", {
user: req.user,
blogs: allBlogs,
});
} catch (error) {
console.log(error);
}
});
app.use("/user", userRouter);
app.use("/blog", blogRouter);
// Start the server
app.listen(port);