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. SAS学习笔记41 宏变量存储及间接引用

    Macro Variables存储在“Symbol Table”中.它是由Macro Processor在SAS启动时自动创建并维护的.SAS提供了一张视图来供我们查看Symbol Table中的内容 ...

  2. 一个农民工自学java找到工作的励志故事

    <!-----------------------------------------------------------------------------摘自网络-------------- ...

  3. JS OOP -04 JS中的公有成员,私有成员和静态成员

    JS中的公有成员,私有成员和静态成员 a.实现类的公有成员 b.实现类的私有成员 c.实现类的静态成员 a.实现类的公有成员 之前定义的任何类型成员都属于公有成员的范畴,该类的任何实例都对外公开这些属 ...

  4. Windows 服务 安装后自启动

    [RunInstaller(true)] public partial class ProjectInstaller : System.Configuration.Install.Installer ...

  5. 【原创】大叔经验分享(93)centos安装oracle客户端

    1 下载客户端 官方下载地址:https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.ht ...

  6. opencv 加载pb

    1.错误1         Tensor's data type is not supported the type of Mul  is DF_Float 2.  错误2 type == " ...

  7. Ubuntu安装telnet

    安装 # sudo apt-get install xinetd telnetd 配置 -> 加入以下选项 # sudo vi /etc/inetd.conf telnet stream tcp ...

  8. ffmpeg转MP4文件为m3u8格式

    第一种转换命令 #转mp4为ts ffmpeg -y -i D:\videos\BgFCWkn00qPBmWVzIEf0eQjaekx0oRjlk9VY2PcR.mp4 -vcodec copy -a ...

  9. Cannot determine value type from string 'xxxxxx'

    Cannot determine value type from string 'xxxxxx' 查了一下,意思就是字段和属性名没有对上. 反复查看代码,字段名和属性名一致. 最后翻阅资料得知是因为构 ...

  10. MyBaties --day1

    今天就来聊聊这个东西 今天的主角相信大家也看到了 MyBaties  是个什么东东 它有什么作用?怎么用?好不好用今天来说说 .说到MyBaties 就不得不谈到JDBC大家可能都知道 JDBC用来操 ...