-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (32 loc) · 1.11 KB
/
app.js
File metadata and controls
40 lines (32 loc) · 1.11 KB
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
import express from "express";
import path from "path";
import userRoute from "./routes/user.js";
import blogRoute from "./routes/blog.js";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
import { checkForAuthenticationCookie } from './middlewares/auth.js';
import Blog from "./models/blog.js";
import dotenv from "dotenv";
dotenv.config({
path: './.env'
})
const app = express();
const PORT = process.env.PORT || 8002;
mongoose.connect(process.env.MONGODB_URI)
.then(() => console.log("MongoDb connected"));
app.set("view engine", "ejs");
app.set("views", path.resolve("./views"));
app.use(express.urlencoded({ extended: false}));
app.use(cookieParser());
app.use(checkForAuthenticationCookie("token"));
app.use(express.static(path.resolve('./public'))); // for render picture set express.static
app.get("/", async (req, res) => {
const allBlogs = await Blog.find({});
res.render("home", {
user: req.user,
blogs: allBlogs
});
});
app.use("/user", userRoute);
app.use("/blog", blogRoute);
app.listen(PORT, () => console.log(`Server Started at PORT:${PORT}`));