-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (45 loc) · 1.48 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const express = require("express");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const dotenv = require("dotenv").config();
const path = require("path");
const favicon = require("serve-favicon");
const app = express();
const blogRoutes = require("./routes/blogRoutes");
const authRoutes = require("./routes/authRoutes");
const commentRoutes = require("./routes/commentRoutes");
const { checkUser } = require("./middleware/authMiddleware");
const PORT = 3000;
const dbURI = `mongodb+srv://${process.env.DBUser}:${process.env.DBPass}@node-blog-project.gycq4so.mongodb.net/?retryWrites=true&w=majority`;
//setting up view engine
app.set("view engine", "ejs");
//using static middleware to acces static files
app.use(express.static("public"));
app.use(favicon(path.join(__dirname, "public/icons", "favicon.ico")));
app.use(express.json());
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
mongoose
.connect(dbURI)
.then((result) => {
app.listen(PORT, function () {
console.log(`listening on http://localhost:${PORT}/`);
});
})
.catch((err) => {
console.log(err);
});
//routes
app.get("*", checkUser);
app.get("/", (req, res) => {
res.redirect("/blogs");
});
app.get("/about", (req, res) => {
res.render("about", { title: "About Us" });
});
//blog routes
app.use(blogRoutes, authRoutes, commentRoutes);
//middleware to send 404
app.use((req, res) => {
res.status(404).render("404", { title: "404" });
});