express use method code example
Example 1: expressjs query params
console.dir(req.query.q)
console.dir(req.query.order)
console.dir(req.query.shoe.color)
console.dir(req.query.shoe.type)
console.dir(req.query.color)
Example 2: widlicard in express router
app.get('/ab?cd', function (req, res) {
res.send('ab?cd')
})
app.get('/ab+cd', function (req, res) {
res.send('ab+cd')
})
app.get('/ab*cd', function (req, res) {
res.send('ab*cd')
})
app.get('/ab(cd)?e', function (req, res) {
res.send('ab(cd)?e')
})
app.get(/a/, function (req, res) {
res.send('/a/')
})
app.get(/.*fly$/, function (req, res) {
res.send('/.*fly$/')
})
Example 3: express router middleware
var express = require('express')
var app = express()
var router = express.Router()
router.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})
router.use('/user/:id', function (req, res, next) {
console.log('Request URL:', req.originalUrl)
next()
}, function (req, res, next) {
console.log('Request Type:', req.method)
next()
})
router.get('/user/:id', function (req, res, next) {
if (req.params.id === '0') next('route')
else next()
}, function (req, res, next) {
res.render('regular')
})
router.get('/user/:id', function (req, res, next) {
console.log(req.params.id)
res.render('special')
})
app.use('/', router)