-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·72 lines (54 loc) · 1.41 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
// ----- Load plugins
const express = require("express");
const https = require("https");
const _ = require("lodash");
// ----- Create express instance
const app = express();
const port = 3000;
// ----- Use express body parser
app.use(express.urlencoded({extended: true}));
app.use(express.json());
// ----- Load public files
app.use(express.static(`${__dirname}/public`));
// ----- Set EJS
app.set("view engine", "ejs");
// ----- Global variables
const posts = []
const opts = {
posts: posts
}
// ----- Get-Post for main page
app.get("/", (req, res) => {
res.render("home", opts);
})
app.get("/about", (req, res) => {
res.render("about")
})
app.get("/contact", (req, res) => {
res.render("contact")
})
app.get("/compose", (req, res) => {
res.render("compose")
})
app.get("/posts/:postId", (req, res) => {
let currentPosts = posts.map( post => _.kebabCase(post.title) );
let postId = _.kebabCase(req.params.postId);
if (currentPosts.includes(postId)) {
let reqPost = posts[currentPosts.indexOf(postId)];
res.render("post", reqPost);
}
})
app.post("/", (req, res) => {
if (req.body.newPostTitle != "" && req.body.newPostContent != "") {
let postData = {
title: req.body.newPostTitle,
content: req.body.newPostContent
}
posts.push(postData);
res.redirect("/");
}
});
// ----- Port listener
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
})