-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (34 loc) · 880 Bytes
/
index.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
const express = require('express')
const nunjucks = require('nunjucks')
const app = express()
nunjucks.configure('views', {
autoescape: true,
express: app,
watch: true
})
const ageMiddleware = (req, res, next) => {
if (req.query.age === '') {
return res.redirect('/')
} else {
return next()
}
}
app.use(express.urlencoded({ extended: false }))
app.set('view engine', 'njk')
app.get('/', (req, res) => {
return res.render('age')
})
app.post('/check', (req, res) => {
if (req.body.age >= 18) {
return res.redirect(`/major?age=${req.body.age}`)
} else {
return res.redirect(`/minor?age=${req.body.age}`)
}
})
app.get('/major', ageMiddleware, (req, res) => {
return res.render('major', { age: req.query.age })
})
app.get('/minor', ageMiddleware, (req, res) => {
return res.render('minor', { age: req.query.age })
})
app.listen('3000')