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. Python中下划线的5种含义

    目录 单前导下划线 _var 当涉及到变量和方法名称时,单个下划线前缀有一个约定俗成的含义. 它是对程序员的一个提示 - 意味着Python社区一致认为它应该是什么意思,但程序的行为不受影响. 单末尾 ...

  2. 【规律】Farey Sums

    [参考博客]: https://blog.csdn.net/meopass/article/details/82952087 Farey Sums 题目描述 Given a positive inte ...

  3. Intellij IDEA中启动多个微服务--开启Run Dashboard管理

    1.找到workspace.xml 2.添加配置 <option name="configurationTypes"> <set> <option v ...

  4. (二十三)JSP指令

    一.JSP指令 1.1 JSP指令 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分 1.2 在JSP 2.0规范中共 ...

  5. Autofac 使用经验

    慢慢总结 基础使用样例,在 Application_Start 中直接使用 //autofac注册 var builder = new ContainerBuilder(); //注册Controll ...

  6. Ocelot + Consul的demo(二)集群部署

    把服务A和服务B接口分别部署在两个ip地址上 修改 services.json文件, { "encrypt": "7TnJPB4lKtjEcCWWjN6jSA==&quo ...

  7. legacy

    int bw = blockDim.x; int bh = blockDim.y; int tx = threadIdx.x%bw; int ty = threadIdx.y%bh; __shared ...

  8. POJ1484(Blowing Fuses)--简单模拟

    题目链接:http://poj.org/problem?id=1484 这题直接简单模拟即可.给你n个容器,m个操作,最大容量C.模拟每一个对器件的开关操作.如果原来是关闭的,则打开,同时最大功耗加上 ...

  9. 基于【 centos7】五 || GitLab环境搭建

    一.基于Docker部署GitLab环境搭建 1.下载镜像 docker pull beginor/gitlab-ce:11.0.1-ce.0 2.创建GitLab 的配置 (etc) . 日志 (l ...

  10. 使用.bat 批量将部分文件迁移到新的路径下

    1.建一个11.bat ,里面写  :   dir  /b /s  >1111.txt 保存后运行 即将所有的文件路径保存到1111.txt中 2.可以将获取的路径复制到execl中,找到自己需 ...