要了解Router我们需要先知道到Application,首先,每一个express实例本身内部就内建了router,所以我们先从简单的下手,先使用application;另外这里我们只选择get方法,作为我们Router.Method, 之所以使用get是因为它足够简单;

精确匹配形式

1. get有很多种用法

 
  1. var express = require("express");

  2. var app = express();

  3. app.get("/example/c",function(req, res){

  4. res.send("Your url is /example/c");

  5. });

  6. app.listen(3000);

2. 参数形式

 
  1. var express = require("express");

  2. var app = express();

  3. app.get('/user/:userid',function(req, res){

  4. res.send(req.params.userid);

  5. });

  6. app.listen(3000,function(){console.log("server is listening on port 3000")});

参数形式另外的高级应用

3. 正则表达式的形式

 
  1. var express = require('express');

  2. var app = express();

  3. app.get(/example/,function(req, res){

  4. res.send('it is use regular expression');

  5. });

  6. app.listen(3000);

4.  神秘的参数 next

 
  1. var express = require('express');

  2. var app = express();

  3. var func1 = function(req, res, next){

  4. console.log("this is first func");

  5. next();

  6. }

  7. var func2 = function(req, res, next){

  8. //不能这么写,因为这样就相当于后面有设置了一遍head

  9. //res.send("this second func");

  10. console.log("this is second func");

  11. next();

  12. }

  13. var func3 = function(reg,res){

  14. console.log("this is thrid func");

  15. res.send("this is in the func3, end");

  16. }

  17. //使用方式1

  18. app.get("/example/",func1,func2,func3);

  19. //使用方式2

  20. app.get("/example/",[func1,func2,func3]);

匹配好之后就是res

res也有很多种形式

比如jsonp

 
  1. var express = require('express');

  2. var app = express();

  3. app.get('/example/defaultCallback?callback=foo',function(req, res){

  4. res.jsop({"message":"this is default callback"});

  5. });

  6. app.get('/example/customizeCallback?cb=foo2',function(req, res){

  7. app.set("jsonp callback name", 'cb');

  8. res.jsonp({"message":"this is customize callback"});

  9. })

  10. app.listen(3000);

默认情况下:http://localhost:3000/example/b?callback=foo

指定情况下:http://localhost:3000/example/c?cb=foo2

比如redirect

 
  1. app.get("/example/d", function(req, res) {

  2. var ua = req.get("user-agent");

  3. if (!!ua && ua.toLowerCase().match(/android|ipad|iphone|ipod/)) {

  4. console.log("this is mb");

  5. res.redirect("http://m.browser.baidu.com/mb");

  6. } else {

  7. console.log("This is pc");

  8. res.redirect("http://m.browser.baidu.com/pc");

  9. }

  10. });

http://localhost:3000/example/d

在chrome里面,打开开发者模式,切换模拟器。

参数形式高级应用

 
  1. var express = require('express');

  2. var app = express();

  3. app.get('/user/:userage/:userid', function(req, res, next) {

  4. console.log("in get method: userid:", req.params.userid);

  5. console.log("in get method: userage:", req.params.userage);

  6. next();

  7. });

  8. app.param("userage", function(req, res, next, value, key) {

  9. console.log("in param key:", key);

  10. console.log("in param value:", value);

  11. next();

  12. });

  13. app.param("userid", function(req, res, next, value, key) {

  14. console.log("in param key:", key);

  15. console.log("in param value:", value);

  16. next();

  17. });

  18. app.get('/user/:userid/:userage', function(req, res, next) {

  19. res.send("userid and userage are:", req.params.userid, req.params.userage);

  20. });

  21. app.listen(3000);

前后顺序与app.param的顺序无关,只与

app.get('/user/:userage/:userid', function(req, res, next) 的顺序有关。

OK,Application基本学习完毕了,我们就来说一下Router,其实没有什么大不同,基本上是一致的。

Router有几种参数形式:

1. function(func){}

2. function(url, function){}

第1种主要是针对于这个路由下的所有情况,都会使用经过这个回调函数的处理。

第2种情况是针对于这个路由下的指定的地址,才会触发回调函数的处理。

以下上干货:

 
  1. var express = require("express");

  2. var app = express();

  3. var router = express.Router();

  4. router.use(function(req, res, next) {

  5. console.log('%s %s %s', req.method, req.url, req.path);

  6. next();

  7. });

  8. router.use(express.static(__dirname + "/bar"), function(req, res, next) {

  9. next();

  10. });

  11. router.use(function(req, res) {

  12. res.send("Hello world");

  13. });

  14. app.use('/foo', router);

  15. app.listen(3000);

因为在app.use中已经制定了父级目录的地址,所以router只需要针对自己目录进行处理即可。

本文涉及的学习资源来源于

http://expressjs.com/en/4x/api.html#router.METHOD

Express 入门之Router - worldtree_keeper的专栏 - CSDN博客相关推荐

  1. Docker安装elasticsearch-head监控ES步骤 - gmijie的专栏 - CSDN博客

    Docker安装elasticsearch-head监控ES步骤 - gmijie的专栏 - CSDN博客 原文:Docker安装elasticsearch-head监控ES步骤 - gmijie的专 ...

  2. NIOS II 常见问题总结FAQ - xiangyuqxq的专栏 - CSDN博客

    [NIOS II 常见问题总结FAQ - xiangyuqxq的专栏 - CSDN博客] https://blog.csdn.net/xiangyuqxq/article/details/726900 ...

  3. linux ramdisk 工具,Linux下用Busy Box制作Ramdisk全过程(转帖+部分完善) - 薛正华的专栏 - CSDN博客.pdf...

    Linux下用BusyBox制作Ramdisk全过程(转帖部分完善)-薛正华的专栏-CSDN博客 Linux下用Busy Box制作Ramdisk全过程(转帖+部分完善) 收藏 /software_z ...

  4. java支持html5视频流技术Pseudostreaming – Roden的专栏 – CSDN博客

    文章目录[隐藏] java支持html5视频流技术Pseudostreaming 首页博客学院 下载论坛APP 问答商城活动VIP会员专题招聘ITeyeGitChat 图文课 写博客 消息 登录注册 ...

  5. oracle表的历史数据转储过程,C#连接Oracle数据库通过存储过程操作数据库 - cuizm的专栏 - CSDN博客...

    C#连接Oracle数据库通过存储过程操作数据库 收藏 此文于2011-06-07被推荐到CSDN首页 此文于2011-06-08被推荐到CSDN首页 如何被推荐? 之前笔者一直用C#连接SQL Se ...

  6. kafka 修改分区_Kafka动态调整topic分区partition - russle的专栏 - CSDN博客

    我们在使用kafka时,初期创建时所指定topic属性需要修改,如何动态修改kafka属性?kafka提供了命令行工具-kafka-topics.sh. kafka-topics.sh工具介绍 kaf ...

  7. mysql 多表关联建模_(四)多对多模式 - 数据库模型设计专栏 - CSDN博客

    连载之5 原创:胖子刘(转载请注明出处及作者,谢谢.)(四)多对多模式 多对多模式,也是比较常见的一种数据库设计模式,它所描述的两个对象不分主次.地位对等.互为一对多的关系.对于A表来说,一条记录对应 ...

  8. JM8.5中的7种宏块模式问题 - zhoujunming的专栏 - CSDN博客

    JM8.5中的7种宏块模式问题 收藏 Outline: 1.  CFG文件中有关可变尺寸宏块模式的相关选项 2.  7种宏块模式对应的数值常量 3.  7种宏块模式被分成宏块和亚宏块 4.  如何对宏 ...

  9. android sensor源码,阅读android有关sensor的源码总结 - JerryMo06的专栏 - CSDN博客

    虽然这篇文章写得很差,因为赶时间,所以就匆匆忙忙地写出来自己作一个笔记.但是我想对大家应该有一点帮助. 1.有关sensor在Java应用程序的编程(以注册多个传感器为例,这程序是我临时弄出来的,可能 ...

最新文章

  1. 图片下方出现几像素的空白间隙
  2. sun.jersey使用Jackson转换数据
  3. 零基础python入门编程全栈量化ai 百度云_Python编程量化AI全栈零基础入门帖子详情 - 网易云课堂...
  4. [Leetcode] Merge Sorted Array 合并数组
  5. python中a=a+2与a+=2的区别
  6. VTK:Utilities之FullScreen
  7. No resource found that matches the given name 'android:Widget.Material.ActionButton'.
  8. 测试动态视力软件叫什么影响吗,动态视力
  9. 面试 | 你说你熟悉MySql,那你就来谈谈InnoDB如何解决幻读的?
  10. 3分钟Tips:正定矩阵与对称矩阵的关系
  11. Unicode字符编码分布表――语言文字类
  12. 计算机管理丢失computer文件,Win7弹框提示找不到Computer Management.lnk文件怎么办?...
  13. webpack的copy-webpack-plugin、patterns
  14. 牛刀:中国未来房价基本走势…
  15. 做市商策略(Market Making Strategy)
  16. post 防篡改_Cookie防篡改机制
  17. Mac VSCode OpenGL环境搭建
  18. Docker容器内存占用过高解决方法
  19. Linux LVS 负载均衡群集
  20. 系统提示服务器错误 请联系管理员,金蝶软件K3 主控台登录提示:应用服务器连接到一个错误的数据库,请与系统管理员联系...

热门文章

  1. 未命名语句块中的声明
  2. excel 公式 单引号 concat_从Excel的数据类型说Python
  3. asp.net mvc项目实例_降龙-第13章:MVC开发准备
  4. 惠普图形计算机游戏,惠普发布Victus by HP 16 - 一款适合PC游戏新人的游戏笔记本...
  5. java复制的函数会报错,2 面试题之面向对象
  6. csp怎么给线条描边_PS的四种“描边”方式你都知道吗?Photoshop小知识
  7. php与mysql列表_PHP+Mysql+jQuery实现的查询和列表框选择
  8. GPU Gems2 - 9 S.T.A.L.K.E.R.中的延迟着色(Deferred Shading in S.T.A.L.K.E.R.)
  9. linux 的文件软链接隐藏,Linux inode及硬链接软链接详解
  10. OpenCV学习——轮廓检测