Skip to content

Commit 8a83e4e

Browse files
author
codingChewie
committed
Statelessness, Setting and Reading Cookies
1 parent 774d778 commit 8a83e4e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const express = require('express')
22
const eyes = require('eyespect')
3+
const cookieParser = require('cookie-parser')
34

45
const app = express()
56

67
app.use(express.urlencoded({ extended: false }))
8+
app.use(cookieParser())
79

810
app.set('view engine', 'pug')
911

@@ -18,10 +20,13 @@ app.get('/cards', (req, res) => {
1820
})
1921

2022
app.get('/hello', (req, res) => {
21-
res.render('hello')
23+
res.render('hello', {
24+
name: req.cookies.username,
25+
})
2226
})
2327

2428
app.post('/hello', (req, res) => {
29+
res.cookie('username', req.body.username)
2530
res.render('hello', {
2631
name: req.body.username,
2732
})

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"homepage": "https://github.com/codingChewie/express-basics#readme",
2323
"dependencies": {
24+
"cookie-parser": "^1.4.4",
2425
"express": "^4.17.1",
2526
"pug": "^2.0.4"
2627
},

0 commit comments

Comments
 (0)