-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (85 loc) · 2.31 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Title : Blog Site MEN
* Author : Kean Duque
* Description : Blog App MEN
*/
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const _ = require("lodash");
const moment = require("moment");
const connectDB = require("./db");
const date = require("./libs/date");
const blog = require("./controllers/posts");
const app = express();
const PORT = process.env.PORT || 3000;
mongoose.set("strictQuery", false);
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true })); // postman Body x-www-form-urlencoded - JSON
app.use(express.static("public"));
//Connect to DB
connectDB()
.then(() => {
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
})
.catch((err) => console.log(err));
const posts = [];
//Server Routers
app.get("/", async (req, res) => {
await blog
.displayPost()
.then((posts) => {
res.render("home", {
homeContent: blog.homeStartingContent,
postedList: posts,
moment: moment,
});
})
.catch((err) => console.log(err));
});
app.get("/about", (req, res) => {
res.render("about", { aboutContent: blog.aboutContent });
});
app.get("/contact", (req, res) => {
res.render("contact", { contactContent: blog.contactContent });
});
app.get("/compose", async (req, res) => {
await res.render("compose");
});
app.post("/compose", async (req, res) => {
const post_title = req.body.post_title;
const postObj = {
title: _.lowerCase(post_title),
author: _.capitalize(req.body.post_author),
date: date.getDate(req.body.post_date),
content: req.body.post_content,
titleURI: _.kebabCase(post_title),
};
await blog.insertPost(postObj, post_title, res);
});
app.get("/posts/:postTitle", async (req, res) => {
const postTitleParam = _.lowerCase(req.params.postTitle);
await blog
.displayPost({ title: postTitleParam })
.then((post) => {
const postTitle = _.lowerCase(post.title);
if (postTitle === postTitleParam) {
res.render("post", {
title: _.capitalize(post.title),
content: post.content,
});
}
})
.catch((err) => console.log(err));
});
app.get("/posts", async (req, res) => {
await blog
.displayPost()
.then((posts) => {
res.render("posts", {
postedList: posts,
});
})
.catch((err) => console.log(err));
});