-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (86 loc) · 4.5 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
98
99
100
101
102
103
104
105
106
107
108
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const express = require("express");
const mongoose = require("mongoose");
const _ = require("lodash");
const Post = require("./models/post");
const dbUrl = process.env.DB_URL || 'mongodb://localhost:27017/blog';
mongoose.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Database connected!");
});
const port = process.env.PORT || 3000;
const homeStartingContent = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis." +
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis." +
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis.";
const aboutContent = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis." +
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis." +
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis.";
const contactContent = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis." +
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis." +
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum dolorem nostrum pariatur sed saepe, similique iste corrupti delectus aliquam officia alias inventore distinctio laboriosam in ea debitis, harum voluptatum perferendis.";
const app = express();
app.set('view engine', 'ejs');
app.use(express.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", async (req, res)=>{
const post = await Post.find({});
res.render("home", {homeStartingContent, post});
});
app.get("/about", (req, res)=>{
res.render("about", {aboutContent});
});
app.get("/contact", (req, res)=>{
res.render("contact", {contactContent});
});
app.get("/compose", (req, res)=>{
res.render("compose");
});
app.post("/compose", async (req, res)=>{
const post = new Post({
title: req.body.postTitle,
content: req.body.postText
});
await post.save();
res.redirect("/");
});
app.get("/posts/:postName", async (req, res)=>{
try{
const post = await Post.find({title: req.params.postName});
res.render("post", {title: post[0].title, content: post[0].content});
}
catch(err){
console.log(err);
res.render("404");
}
});
app.get("/posts/:postName/edit", async (req, res)=>{
try{
const post = await Post.find({title: req.params.postName});
res.render("edit", {title: post[0].title, content: post[0].content});
}
catch(err){
console.log(err);
res.render("404");
}
});
app.post("/posts/:postName/edit", async (req, res)=>{
await Post.updateOne({title: req.params.postName}, {title: req.body.postTitle, content: req.body.postText});
res.redirect(`/posts/${req.body.postTitle}`);
});
app.get("/posts/:postName/delete", async (req, res)=>{
await Post.deleteOne({title: req.params.postName});
res.redirect("/");
});
app.get("/secret", (req, res)=>{
res.render("secret");
});
app.get("*", (req, res)=>{
res.render("404");
});
app.listen(port, function(){
console.log(`Server started at port ${port}`);
});