express next('route) code example
Example 1: 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)
Example 2: express() vs express.router()
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('GET handler for /dogs route.');
});
router.post('/', function(req, res) {
res.send('POST handler for /dogs route.');
});
module.exports = router;