-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (32 loc) · 1.06 KB
/
app.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
require("dotenv").config();
const express = require("express")
const path = require("path")
const mongoose = require("mongoose")
const userRoute = require("./routes/user")
const cookieParser = require("cookie-parser");
const checkForAuthenticationCookie = require("./middlewares/authentication")
const blogRoute = require("./routes/blogs")
const Blog = require("./model/blog")
const app = express();
const PORT = process.env.PORT ;
//Connection
mongoose.connect(process.env.MONGO_URL).then((e)=>{console.log("MongoDB connected")});
app.set("view engine", "ejs")
app.set("views", path.resolve("./views"))
//Middlewares
app.use(express.urlencoded({extended: false}))
app.use(cookieParser());
app.use(checkForAuthenticationCookie("token"))
app.use(express.static(path.resolve("./public")));
//Routes
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);
//Listener
app.listen(PORT, ()=>{console.log(`Server Started:`,PORT)});