Skip to content

Commit 0d3c74e

Browse files
author
codingChewie
committed
Redirects
1 parent 8a83e4e commit 0d3c74e

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ app.use(cookieParser())
99

1010
app.set('view engine', 'pug')
1111

12-
app.get('/', (req, res) => res.render('index'))
12+
app.get('/', (req, res) => {
13+
const name = req.cookies.username
14+
15+
res.render('index', { name })
16+
})
1317

1418
app.get('/cards', (req, res) => {
1519
eyes.inspect(res.locals, 'res.locals')
@@ -27,9 +31,7 @@ app.get('/hello', (req, res) => {
2731

2832
app.post('/hello', (req, res) => {
2933
res.cookie('username', req.body.username)
30-
res.render('hello', {
31-
name: req.body.username,
32-
})
34+
res.redirect('/')
3335
})
3436

3537
app.listen(3000, () => console.log('The app is running on port 3000'))

views/hello.pug

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
extends layout.pug
22

33
block content
4-
if name
5-
h2 welcome, #{name}
6-
else
7-
form(action='/hello', method='POST')
8-
label Please enter name
9-
input(type='text', name='username')
10-
button(type='submit') Submit
4+
form(action='/hello', method='POST')
5+
label Please enter name
6+
input(type='text', name='username')
7+
button(type='submit') Submit

views/index.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ extends layout.pug
22

33
block content
44
section#content
5-
h2 Welcome, student
5+
h2 Welcome, #{name}!
66

0 commit comments

Comments
 (0)