-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
38 lines (31 loc) · 1.08 KB
/
controller.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
/* globals app */
import {validate} from 'validator'
app.get('/some-router', (req, res) => {
res.render('login.whatever', {errors: {}})
})
app.post('/some-route', (req, res) => {
let {email, invite, password0, password1} = req.body || {}
let output = validate({email, invite, password0, password1})
output.cata(errors => {
res.render('login.whatever', {errors})
}, data => {
// do something with data
res.redirect('some-other-route')
})
})
app.post('/some-async-route', async (req, res) => {
let {email, invite, password0, password1} = req.body || {}
/**
* Here, validate returns a Validation wrapped in a Promise, so you need to
* unwrap it with await before using its methods.
* @type {Validation.<ValidationError, Object>}
*/
let output = await validate({email, invite, password0, password1})
output.cata(errors => {
// re-render the form with errors
res.render('login.whatever', {errors})
}, data => {
// do something with data
res.redirect('some-other-route')
})
})