router-level middleware works in the same way as application-level middleware, except it is bound to an instance of  express.Router().

code:

var express = require('express');
var app = express();
var router = express.Router(); // a middleware function with no mount path. This code is executed for every request to the router
router.use(function (req, res, next) {
console.log('Time:', Date.now());
next()
}); // a middleware sub-stack shows request info for any type of HTTP request to the /user/:id path
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()
}); // a middleware sub-stack that handles GET requests to the /user/:id path
router.get('/user/:id', function (req, res, next) {
// if the user ID is 0, skip to the next router
if (req.params.id === '') next('route');
// otherwise pass control to the next middleware function in this stack
else next()
}, function (req, res, next) {
// render a regular page
res.send('regular');
}); // handler for the /user/:id path, which renders a special page
router.get('/user/:id', function (req, res, next) {
console.log(req.params.id);
res.send('special');
}); app.set('view engine','jade');
// mount the router on the app
app.use('/', router); app.listen();

result:

learning express step(九)的更多相关文章

  1. learning express step(十四)

    learning express error handle code: const express = require('express'); const app = express(); const ...

  2. learning express step(十三)

    learning express error handle code: const express = require('express'); const app = express(); app.g ...

  3. learning express step(十二)

    learning express view engine function const express = require('express'); const app = express(); app ...

  4. learning express step(十一)

    learning express.Router() code: const express = require('express'); const app = express(); var route ...

  5. learning express step(五)

    learning  express  middleware var express = require('express'); var app = express(); var myLogger = ...

  6. learning express step(四)

    learning express route function const express = require('express'); const app = express(); app.get(' ...

  7. learning express step(八)

    To skip the rest of the middleware functions from a router middleware stack, call next('route') to p ...

  8. learning express step(七)

    Route handlers enable you to define multiple routes for a path. The example below defines two routes ...

  9. learning express step(六)

    code: use application middleware var express = require('express'); var app = express(); app.use(func ...

随机推荐

  1. S03_CH10_DMA_4_Video_Stitch视频拼接系统

    S03_CH10_DMA_4_Video_Stitch视频拼接系统 10.1概述 注意:本课程和上一课程<S03_CH09_DMA_4_Video_Switch视频切换系统>基本相同,不同 ...

  2. c++学习(三)------static数据与成员函数

    疑惑: static类型成员是类的全局变量,所有类的实例都享有这个变量,或者说这个变量不属于任何一个类的实例. static类型变量可以为private,或public或其他(static数据可以被继 ...

  3. JS 03事件

    <script type="text/javascript"> function getUserInput() { //获取用户输入的内容 var val = docu ...

  4. (错误)Lucene工具Luck启动错误

    启动luke命令行下图错误 错误原因:luke版本和lucene版本不匹配,lucene5.3.0版本必须用luke5.3.0版本才能打开索引. 解决方法:更新luke版本即可. luke 所有版本下 ...

  5. Java数据结构Vetor

    Java数据结构Vector /** * <html> * <body> * <P> Copyright JasonInternational</p> ...

  6. WebSocket协议探究(三):MQTT子协议

    一 复习和目标 1 复习 Nodejs实现WebSocket服务器 Netty实现WebSocket服务器(附带了源码分析) Js api实现WebSocket客户端 注:Nodejs使用的Socke ...

  7. 使用帅气的cordic算法进行坐标系互转及log10的求解

    参考博客 https://blog.csdn.net/u010712012/article/details/77755567 https://blog.csdn.net/Reborn_Lee/arti ...

  8. Android Parcelable 序列化复杂数据结构

    参考博文 http://blog.csdn.net/yangzl2008/article/details/7593226 由于项目需要,Activity之间要传递一个特别复杂的数据结构对象,由于以前序 ...

  9. js 跳出for/for in/each/for each 循环

    写本文原因:最近用到了for in,用return true跳出本次循环,执行下次循环, 结果发现程序没有预期效果,经过调试发现误用了return true, 特此笔记,欢迎指正. 注意:return ...

  10. X宝个人支付到账

    扫码登录,能看懂的我就不多说了,封了我多少篇文章了!!!!X宝个人到账通知.