-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
90 lines (75 loc) · 3.5 KB
/
app.js
File metadata and controls
90 lines (75 loc) · 3.5 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const _ = require('lodash');
const mongoose = require('mongoose');
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
const homeContent = "The homepage of your site is the first introduction each visitor will have to your business. Before they make up their mind to become a customer, they’ll review your homepage to get an idea of what you sell, why that matters to them, and how they can benefit from what you have to offer."
+ "Make a brilliant first impression with a homepage that incorporates the elements outlined above. And for more inspiration, check out stunning examples of homepages by downloading the free look book below."
const aboutContent = "What’s your writing process like? When a writer hears this question, their first thought it, “None of your business.” Or, “It’s mine and I don’t want to tell you.” Even without the defense, the answer is, “Well, it changes. It depends on the project, the subject, my mood…" +
+ "The writing process is personal to the writer. They may have one tried-and-true process they swear by or several they choose from. Even the weather can play a role in how you write – where you plant yourself to type and how long you work for, for example. Planners approach writing strategically. “Pansters” – people who fly by the seat of their pants – don’t like constraints."
const contactContent = "This Contact Us page is for a marketing agency that works directly with businesses. Since it knows its audience, Brandaffair encourages visitors to 'have a talk' one-on-one rather than providing a one-way communication channel via support resources."
main().catch(err => console.log(err));
async function main() {
mongoose.set('strictQuery', true);
await mongoose.connect('mongodb://127.0.0.1:27017/blogApp');
}
const postSchema = mongoose.Schema({
title: String,
content: String
});
const Post = new mongoose.model("Post", postSchema);
let post = new Post({
title: "",
content: ""
});
app.get("/", function (req, res) {
Post.find(function (err, posts) {
if (!err) {
console.log("No error");
res.render("home.ejs", { content: homeContent, post: posts })
}
})
})
app.get("/about", function (req, res) {
res.render("about.ejs", { content: aboutContent })
})
app.get("/contact", function (req, res) {
res.render("contact.ejs", { content: contactContent })
})
app.get("/compose", function (req, res) {
res.render("compose.ejs", { content: contactContent })
})
app.get("/posts/:postId", function (req, res) {
let postId = req.params.postId;
postId = postId.trim();
Post.findOne({ _id: postId }, function (err, foundPost) {
if (!err) {
res.render("post.ejs", { title: foundPost.title, postContent: foundPost.content });
};
});
});
app.get("/posts", function (req, res) {
Post.find(function (err, foundPosts) {
res.render("posts.ejs", { posts: foundPosts })
})
})
app.post("/compose", function (req, res) {
let post = new Post({
title: req.body.postTitle,
content: req.body.postArea
});
post.save(function (err) {
if (!err) {
res.redirect("/");
} else {
console.log(err);
}
});
})
app.listen(3000, function () {
console.log("This server is running on 3000 port");
})