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. 模糊查询库的存储过程(SQLServer)

    --查询带有自己需要内容的存储过程 SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROU ...

  2. 解决springboot乱码和window cmd乱码

    @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registration = ...

  3. php底层源码之数组

    数组key和value的限制条件 <?php $arr = array( 1 => 'a', "1" => "b", 1.5 => &q ...

  4. (十一)shiro与ssm整合

    所有代码在:here pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...

  5. (一)XML基础(1)

    一.什么是XML? XML是指可扩展标记语言(eXtensible MarkupLanguage),它是一种标记语言.它被设计的宗旨是描述数据(XML),而非显示数据(HTML). 目前遵循的是W3C ...

  6. eclipse智能提示报错(to avoid the message, disable the...)

    然后这里可能会再报别的错误  我的做法是   再重新将 code recommenders 打开 ********************************** 为什么会出现 这样错误呢 ? 简 ...

  7. 前端知识总结--css用div画环形圆

    如何用最少的div画最多的环形?如下图所示最少需要多少个div? 暂时想到的利用div的边框.内外阴影及befor和after的伪元素实现 以下代码可以实现上图效果: <style> di ...

  8. ES6箭头函数及this指向

    箭头函数(=>):函数简写 无参数:() => {} 单个参数:x => {} 多个参数:(x, y) => {} 解构参数:({x, y}) => {} 嵌套使用:部署 ...

  9. centos根目录扩容,home目录减小容量

    参考: https://blog.csdn.net/evandeng2009/article/details/49814097 主要命令: 15 cd / 16 ll 17 mkdir backup ...

  10. h5 移动端开发自适应 meta name="viewport"的使用总结

    本文系个人理解,可能有误差,仅供参考,谨慎采纳! 布局视口: 系统自带 一般大于屏幕宽度 理想宽度:  设置页面的viewport 的一个宽度,使不同的手机的布局视口宽度尽量接近可视窗口的值: 可视视 ...