-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (52 loc) · 1.94 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
const express = require("express")
const _ = require("lodash")
const cors = require('cors')
const homeBeginningContent = "This is another blog using the London App Brewery, Dr Angela Yu's Udemy course material. Also with some materials from Hamburg Coding School, Node.js course.";
const aboutContent = "This blog has been created using Node.js, ejs and with Bootstrap as CSS framework. I intend to use this as another experiment and to practice using Node."
const contactPage= "I will probably repeat the trick using the same provider for a contact page. Perhaps, I will also use this to try out a php server. "
const app = express();
app.use(cors());
const port = 3000;
app.use(express.static("public"));
app.set("view engine", "ejs")
app.use(express.urlencoded({extended: false}))
let newBlogs = [];
app.get("/", function (req, res) {
res.render("index",
{intro: homeBeginningContent,
posts: newBlogs}
);
});
app.get("/about", function (req, res) {
res.render("about", {about: aboutContent});
});
app.get("/contact", function (req, res) {
res.render("contact", {contact: contactPage});
});
app.get("/compose", function (req, res) {
res.render("compose");
});
//Here we record when user type something in the iput box, hence req.body.(name of the input name)
app.post("/compose", function(req, res){
let newBlog = {
title: req.body.newTitle,
content: req.body.newContent
}
newBlogs.push(newBlog);
res.redirect("/");
})
app.get('/posts/:postName', (req, res) => {
const requestedTitle = _.lowerCase(req.params.postName);
newBlogs.forEach(function(blog){
const storedTitle = _.lowerCase(blog.title);
if (requestedTitle === storedTitle) {
res.render("post", {
title: blog.title,
content: blog.content
});
}
});
});
app.listen(port, function(){
console.log("listening to port " + port)
})