-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (101 loc) · 4.53 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//jshint esversion: 6
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const _ = require("lodash");
const app = express();
app.set("views", __dirname + "/views");
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
const homeStartingContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eget mi proin sed libero enim sed faucibus turpis. Et malesuada fames ac turpis egestas. Massa tempor nec feugiat nisl pretium fusce id. Donec enim diam vulputate ut pharetra sit amet aliquam id. Tincidunt augue interdum velit euismod in. Tincidunt arcu non sodales neque sodales ut etiam sit amet. Non nisi est sit amet facilisis magna. Nunc lobortis mattis aliquam faucibus purus in massa. Volutpat diam ut venenatis tellus in metus vulputate eu scelerisque. Montes nascetur ridiculus mus mauris vitae. Purus sit amet volutpat consequat mauris nunc. Cursus in hac habitasse platea dictumst.";
const aboutContent = "Pellentesque consectetur congue tempus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam erat volutpat. Cras ac hendrerit neque. Cras eu maximus tellus, sed facilisis justo. Sed sed congue lacus. Cras semper rhoncus ex, vel laoreet justo efficitur nec. Mauris facilisis accumsan nulla, eu euismod nisi eleifend quis";
const contactContent = "Donec sed tellus in augue semper laoreet. Maecenas eleifend augue sed odio porttitor, vitae dignissim eros pharetra. Nulla justo turpis, ornare sit amet nisi vel, euismod semper elit. Nunc nec elit vitae nunc imperdiet finibus scelerisque nec erat. Pellentesque tempus pellentesque magna nec luctus. Praesent et faucibus ante, ut ultrices nunc.";
const mongoose = require('mongoose');
// const connectionString = 'mongodb://localhost:27017/blogDB';
const connectionString = process.env.MONGO_CONNECTION_STRING;
mongoose.connect(connectionString, { family: 4 })
.then(() => {
console.log('Connected to MongoDB');
})
.catch((err) => {
console.log(err);
});
// let posts = [];
const postSchema = {
title: String,
content: String
}
const Post = mongoose.model("Post", postSchema);
app.get("/", function (req, res) {
Post.find({}).then(function (posts) {
res.render('home', { startingContent: homeStartingContent, posts: posts });
})
.catch(function (err) {
console.log(err);
res.status(500).send('Error');
});
// res.render("home", { startingContent: homeStartingContent, posts: posts });
// console.log(posts);
});
app.get("/about", function (req, res) {
res.render("about", { aboutContent: aboutContent });
});
app.get("/contact", function (req, res) {
res.render("contact", { contactContent: contactContent });
});
app.get("/compose", function (req, res) {
res.render("compose");
});
app.post("/compose", function (req, res) {
// console.log(req.body.postTitle);
const post = new Post({
title: req.body.postTitle,
content: req.body.postBody,
});
post.save().then(() => {
res.redirect("/");
}).catch(err => {
console.log(err);
res.status(500).send('Error');
});
});
// app.get("/posts/:postName", function (req, res) {
// // console.log(req.params.postName);
// const requestedTitle = _.lowerCase(req.params.postName);
// posts.forEach(function (post) {
// const storedTitle = _.lowerCase(post.title);
// if (storedTitle === requestedTitle) {
// // console.log("match found");
// res.render("post", { title: post.title, content: post.content });
// }
// });
// });
app.get('/posts/:postId', async function (req, res) {
const requestedPostId = req.params.postId;
try {
const post = await Post.findOne({ _id: requestedPostId });
if (post) {
res.render('post', {
title: post.title,
content: post.content
});
} else {
res.status(404).render('404');
}
} catch (err) {
console.log(err);
res.status(500).send('Error');
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function () {
console.log("server is running on port 3000");
});
// app.listen(3000, function () {
// console.log("server is running on port 3000");
// });