Skip to content

Commit

Permalink
update async example for cookie validation middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ryhinchey committed Apr 6, 2020
1 parent cac7fb4 commit 5ac2647
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions en/guide/writing-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ When you make a request to the root of the app, the app now displays the timesta

Finally, we'll create a middleware function that validates incoming cookies and sends a 400 response if cookies are invalid.

Here's a simple function that validates the presence of certain cookies.
Here's a simple function that validates cookies with an external async service.

```js
function cookieValidator (cookies) {
return new Promise(function (resolve, reject) {
if (!cookies.testCookie) {
reject(new Error('Invalid cookies'))
}

resolve()
externallyValidateCookie(cookies.testCookie)
.then(resolve)
.catch(function () {
reject(new Error('Invalid cookies'))
})
})
}
```
Expand All @@ -169,10 +169,9 @@ var cookieValidator = require('./cookieValidator')

var app = express()

function validateCookies (req, res, next) {
return cookieValidator(req.cookies).then(function () {
next()
})
async function validateCookies (req, res, next) {
await cookieValidator(req.cookies)
next()
}

app.use(cookieParser)
Expand Down

0 comments on commit 5ac2647

Please sign in to comment.