-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (59 loc) · 1.84 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
const express = require("express");
const dotenv = require("dotenv");
dotenv.config();
const bodyParser = require("body-parser");
const _ = require("lodash");
const app = express();
const mongoose = require("mongoose");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true }).then(() => {
console.log("Connected to Db");
});
const postSchema = {
title: String,
content: String,
};
const Post = mongoose.model("Post", postSchema);
app.get("/", function (req, res) {
Post.find({}).then((posts) => {
res.render("home", { startingcontent: homeStartingContent, posts: 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) {
const post = new Post({
title: req.body.postTitle,
content: req.body.postbody,
});
post.save();
res.redirect("/");
});
app.get("/posts/:postid", function (req, res) {
const requestedpostid = req.params.postid;
Post.findOne({ _id: requestedpostid })
.then((post) => {
res.render("post", { posttitle: post.title, postcontent: post.content });
})
.catch(function (err) {
console.log(err);
});
});
const port = process.env.PORT || 3000;
app.listen(port, function (res) {
console.log("server connected succesfully");
});
const homeStartingContent =
"Welcome to your dairy.Site where you can share all your memories";
const aboutContent = "This site helps you save all your memories.";
const contactContent =
"This is the contact page.For any queries contact to [email protected]";