-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (52 loc) · 1.11 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
/* app.js */
// require and instantiate express
const app = require('express')()
// fake posts to simulate a database
const posts = [
{
id: 1,
author: 'John',
title: 'Templating with EJS',
body: 'Blog post number 1'
},
{
id: 2,
author: 'Drake',
title: 'Express: Starting from the Bottom',
body: 'Blog post number 2'
},
{
id: 3,
author: 'Emma',
title: 'Streams',
body: 'Blog post number 3'
},
{
id: 4,
author: 'Cody',
title: 'Events',
body: 'Blog post number 4'
}
]
// set the view engine to ejs
app.set('view engine', 'ejs')
// blog home page
app.get('/', (req, res) => {
// render `home.ejs` with the list of posts
res.render('home', { posts: posts })
})
// blog post
app.get('/post/:id', (req, res) => {
// find the post in the `posts` array
const post = posts.filter((post) => {
return post.id == req.params.id
})[0]
// render the `post.ejs` template with the post content
res.render('post', {
author: post.author,
title: post.title,
body: post.body
})
})
app.listen(8080)
console.log('listening on port 8080')