-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (37 loc) · 1.1 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
const express = require("express");
const dotenv = require("dotenv");
const path = require("path");
const cookieParser = require("cookie-parser");
const connectToDb = require("./db");
const { checkForAuthCookie } = require("./middlewares/authentication");
const Blog = require("./models/blog");
// route
const userRoute = require("./routes/user");
const blogRoute = require("./routes/blog");
// config
connectToDb();
const app = express();
dotenv.config();
const port = process.env.PORT;
// view engine
app.set("view engine", "ejs");
app.set("views", "./views");
// middlewares
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(checkForAuthCookie("token"));
app.use(express.static("./public"));
//
app.use("/user", userRoute);
app.use("/blog", blogRoute);
app.use("/", async (req, res) => {
const allBlogs = await Blog.find({});
res.render("home", {
user: req.user,
blogs: allBlogs,
});
});
app.get("*", function (req, res) {
res.render("404");
});
app.listen(port, () => console.log(`Server started at port: ${port}`));