-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (67 loc) · 1.85 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
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
var _ = require('lodash');
const homeStartingContent = "Daily Journal is a motivational blog where you find new motivational quotes daily, and kickstart your day with enthusiasm and commitment.";
const aboutContent = "This website is created by Harshit Kumar, a developer from India. Currently an engineering student, and available for freelance work. For more details go to contact page.";
const app = express();
// setting up view engine EJS
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
var posts = [];
// root route
app.get("/", (req, res) => {
res.render("home", {
startingContent: homeStartingContent,
posts: posts
});
})
// about page
app.get("/about", (req, res) => {
res.render("about", {
aboutContent: aboutContent
});
})
// contact us page
app.get("/contact", (req, res) => {
res.render("contact");
})
// hidden compose page
app.get("/compose", (req, res) => {
res.render("compose");
})
// hidden compose page
app.post("/compose", (req, res) => {
var post = {
postTitle: req.body.postTitle,
postBody: req.body.postBody
};
posts.push(post);
res.redirect("/");
})
// custom pages
app.get("/posts/:postName", (req, res) => {
var requestedTitle = _.lowerCase(req.params.postName);
posts.forEach(function (post) {
var storedTitle = _.lowerCase(post.postTitle);
if (storedTitle == requestedTitle) {
res.render("post", {
title: post.postTitle,
body: post.postBody
});
}
})
res.render("error");
})
// invalid routes
app.get("*", (req, res) => {
res.render("error");
})
// server port
app.listen(process.env.PORT || 3000, function () {
console.log("Server started on port 3000");
});