learning express step(九)
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(九)的更多相关文章
- learning express step(十四)
learning express error handle code: const express = require('express'); const app = express(); const ...
- learning express step(十三)
learning express error handle code: const express = require('express'); const app = express(); app.g ...
- learning express step(十二)
learning express view engine function const express = require('express'); const app = express(); app ...
- learning express step(十一)
learning express.Router() code: const express = require('express'); const app = express(); var route ...
- learning express step(五)
learning express middleware var express = require('express'); var app = express(); var myLogger = ...
- learning express step(四)
learning express route function const express = require('express'); const app = express(); app.get(' ...
- learning express step(八)
To skip the rest of the middleware functions from a router middleware stack, call next('route') to p ...
- learning express step(七)
Route handlers enable you to define multiple routes for a path. The example below defines two routes ...
- learning express step(六)
code: use application middleware var express = require('express'); var app = express(); app.use(func ...
随机推荐
- Python 的 Mixin 类(转)
转1:https://www.cnblogs.com/aademeng/articles/7262520.html 转2:https://blog.csdn.net/u010377372/articl ...
- 使用vue-cli创建vue工程
在Windows环境下,打开命令行窗口,跳转至想创建工程的路径. 如:D:\MyWork\22_Github\rexel-cn\rexel-jarvis 创建vue工程,命令:vue create r ...
- Docker相关环境全套安装文档兼小技能
Docker相关环境全套安装文档兼小技能 以下环境皆为ubuntu16.04,主要安装docker,docker-compose,docker仓库等. Docker安装 参考官方 A: 有源安装 Ub ...
- 面试经典算法:快速排序Golang实现
Golang快速排序 定义 快速排序由C. A. R. Hoare在1962年提出.快速排序是对冒泡排序的一种改进,采用了一种分治的策略. 基本思想 通过一趟排序将要排序的数据分割成独立的两部分,其中 ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- 第三代PacBio测序技术的测序原理和读长
针对PacBio单分子测序——第三代测序技术的测序原理和读长 DNA基因测序技术从上世纪70年代起,历经三代技术后,目前已发展成为一项相对成熟的生物产业.测序技术的应用也扩展到了生物.医学.制 ...
- [NOIP2018模拟赛10.18]自闭报告
闲扯 这一天,菜鸡RyeCatcher又想起来了被毒瘤题支配的恐惧 今天比较好玩,还是ljy提醒才发现文件夹里有题面...不知道外面的人什么时候才发现 看完了题面,又回到了雅礼啥题也不会写的感觉 T1 ...
- JQuery攻略读书笔记---第2章 数组
2 数组2.8 创建对象数组循环数组2.9 数组排序 2 数组 2.8 创建对象数组 //数组化对象 var student =[ { "role":101, "na ...
- 关于vue.js的部分总结
1.MVVM和MVC的区别: MVVM:是Model-View-ViewModel的简写,即模型-视图-视图模型 模型:后端传递的数据 试图:所看到的页面 视图模型:mvvm模式的核心,它是连接vie ...
- js入门第一篇
简介:JavaScript 运行在客户端(浏览器)是一种客户端语言,javascript的引擎被称为JavaScript引擎,为浏览器的一部分广泛用于客户端的脚本语言 应用场景:网页特效, 服务端开发 ...