Skip to content

Instantly share code, notes, and snippets.

@evanxg852000
Last active January 4, 2019 12:37
Show Gist options
  • Save evanxg852000/bfd58fcae64c5493dc25069eb5729260 to your computer and use it in GitHub Desktop.
Save evanxg852000/bfd58fcae64c5493dc25069eb5729260 to your computer and use it in GitHub Desktop.
app.route((router) => {
router.get('/template', (req, res) => {
let data = {title: 'Home', test:true, items: ['Evan', 'John', 'Jane']}
res.render('index.html', data)
})
// routing with optional parameter
router.get('/hello/:name?', (req, res) => {
let name = req.params['name'] || 'world'
res.end(`Hello ${name}!`)
})
// route prefixing with group
router.group('/settings', (router) => {
router.get('/basic', (req, res) => {
res.end('Basic Settings')
})
router.get('/notification', (req, res) => {
res.end('Notification Settings')
})
})
// example of midleware handler chaining
router.get('/chain/example', [
(req, res, next) => {
req.chain = 'one ->'
next()
},
(req, res, next) => {
req.chain = `${ req.chain} two ->`
next()
},
(req, res) => {
res.end(`${req.chain} three`)
}
]
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment