-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
81 lines (65 loc) · 1.71 KB
/
app.js
File metadata and controls
81 lines (65 loc) · 1.71 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
'use strict';
require('dotenv').config();
const LoremIpsum = require("lorem-ipsum").LoremIpsum;
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const _ = require('lodash');
const lorem = new LoremIpsum({
sentencesPerParagraph: {
max: 8,
min: 4
},
wordsPerSentence: {
max: 16,
min: 4
}
});
const port = process.env.PORT || 3000;
const homStartingContent = lorem.generateParagraphs(2);
const aboutStartingContent = lorem.generateParagraphs(2);
const contactStartingContent = lorem.generateParagraphs(2);
let posts = [];
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('home', {
homeStartingContent: homStartingContent,
posts: posts,
});
});
app.get('/about', (req, res) => {
res.render('about', {
aboutStartingContent: aboutStartingContent,
});
});
app.get('/contact', (req, res) => {
res.render('contact', {
contactStartingContent: contactStartingContent,
});
});
app.get('/compose', (req, res) => {
res.render('compose');
});
app.post('/compose', (req, res) => {
const post = {
title: req.body.postTitle,
body: req.body.postContent,
};
posts.push(post);
res.redirect('/');
});
app.get('/posts/:title', (req, res) => {
posts.forEach((post) => {
if (_.lowerCase(req.params.title) === _.lowerCase(post.title)) {
res.render('post', {
post: post,
});
}
});
});
app.listen(port, () => {
console.log(`Listening on port: ${port}`)
});