NodeJS之express的路由浅析
路由路径和请求方法一起定义了请求的端点,它可以是字符串、字符串模式或者正则表达式。后端在获取路由后,可通过一系列类似中间件的函数去执行事务。
可使用字符串的路由路径:
// 匹配根路径的请求
app.get('/', function (req, res) {
res.send('root');
}); // 匹配 /about 路径的请求
app.get('/about', function (req, res) {
res.send('about');
}); // 匹配 /random.text 路径的请求
app.get('/random.text', function (req, res) {
res.send('random.text');
});
可使用字符串模式的路由路径:
// 匹配 acd 和 abcd
app.get('/ab?cd', function(req, res) {
res.send('ab?cd');
}); // 匹配 /abe 和 /abcde
app.get('/ab(cd)?e', function(req, res) {
res.send('ab(cd)?e');
});
可使用正则表达式的路由路径:
// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function(req, res) {
res.send('/.*fly$/');
});
可以为请求处理提供多个回调函数,其行为类似中间件。唯一的区别是这些回调函数有可能调用 next('route') 方法而略过其他路由回调函数。可以利用该机制为路由定义前提条件,如果在现有路径上继续执行没有意义,则可将控制权交给剩下的路径。
路由句柄有多种形式,可以是一个函数、一个函数数组,或者是两者混合,如下所示
使用一个回调函数处理路由:
app.get('/example', function (req, res) {
res.send('Hello from A!');
});
使用多个回调函数处理路由(记得指定 next 对象):
app.get('/example/b', function (req, res, next) {
console.log('response will be sent by the next function ...');
next();
}, function (req, res) {
res.send('Hello from B!');
});
使用回调函数数组处理路由:
var cb0 = function (req, res, next) {
console.log('CB0');
next();
}
var cb1 = function (req, res, next) {
console.log('CB1');
next();
}
var cb2 = function (req, res) {
res.send('Hello from C!');
}
app.get('/example/c', [cb0, cb1, cb2]);
混合使用函数和函数数组处理路由:
var cb0 = function (req, res, next) {
console.log('CB0');
next();
}
var cb1 = function (req, res, next) {
console.log('CB1');
next();
}
app.get('/example/d', [cb0, cb1], function (req, res, next) {
console.log('response will be sent by the next function ...');
next();
}, function (req, res) {
res.send('Hello from D!');
});
可使用 express.Router 类创建模块化、可挂载的路由句柄。Router 实例是一个完整的中间件和路由系统。
var log=require("./log/log");
var express=require("express");
var app=express();
//首先定义一个路由
var router=express.Router();
//使用中间件
router.use(function firstFunc(req,res,next){
console.log("Time: ",Date.now());
next();
});
//如果是根目录
router.get("/",function(req,res){
res.send("home page");
});
//如果是根目录下的about目录
router.get("/about",function(req,res){
res.send("about page");
});
//使用路由
//可输入http://127.0.0.1:8000/myRoot得到home page
//可输入http://127.0.0.1:8000/myRoot/about得到about page
//在得到返回的page之前,会先执行firstFunc函数
app.use("/myRoot",router);
//开启站点
app.listen(,function(){
log.info("站点开启")
});
通过路由,可以在返回页面之前,先通过中间件执行若干事物,而且这些中间件是当前路由共享的,这样可以省去许多重复代码,增加代码可利用率的有效性。还可以将Router注册为模块导出,代码更加有可读性。
注明:以上学习内容来自:http://www.expressjs.com.cn/guide/routing.html
NodeJS之express的路由浅析的更多相关文章
- nodejs之express静态路由、ejs
1.静态路由与ejs使用 /** *1.安装ejs npm install ejs --save-dev * *2.express 里面使用ejs ,安装以后就可以用,不需要引入 * *3.配置exp ...
- nodejs之express中间件路由使用
1.express 中间件使用 /* * 中间件:就是匹配路由之前和匹配路由之后做的一系列操作 */ var express = require('express'); var app = new e ...
- [转] NodeJS框架express的途径映射(路由)功能及控制
NodeJS框架express的路径映射(路由)功能及控制 我们知道Express是一个基于NodeJS的非常优秀的服务端开发框架,本篇CSSer将提供express框架的route和route co ...
- nodejs之express路由与动态路由
1.快速创建express项目步骤 /** * 1.cd 到项目里面 * 2.npm init --yes 创建package.json文件 * 3.安装express * npm install e ...
- 77.深入理解nodejs中Express的中间件
转自:https://blog.csdn.net/huang100qi/article/details/80220012 Express是一个基于Node.js平台的web应用开发框架,在Node.j ...
- 使用nodejs和express搭建http web服务
目录 简介 使用nodejs搭建HTTP web服务 请求nodejs服务 第三方lib请求post 获取http请求的正文 Express和使用express搭建http web服务 express ...
- NodeJS 框架 Express 从 3.0升级至4.0的新特性
NodeJS 框架 Express 从 3.0升级至4.0的新特性 [原文地址:√https://scotch.io/bar-talk/expressjs-4-0-new-features-and-u ...
- npm install Error:EPROTO: protocol error, symlink '../mime/cli.js' -> '/vagrant/src/nodejs/node_modules/express/node_modules/send/node_modules/.bin/mime'
我在ubuntu上使用npm安装依赖是出现下面错误: npm ERR! Linux 3.13.0-101-genericnpm ERR! argv "/usr/bin/nodejs" ...
- 前端笔记之NodeJS(二)路由&REPL&模块系统&npm
一.路由机制(静态资源文件处理) 1.1 Nodejs没有根目录 MIME类型:http://www.w3school.com.cn/media/media_mimeref.asp 在Apache中, ...
随机推荐
- openwrt的编译方法
1.获取最新包 ./scripts/feeds update -a 2.安装包 ./scripts/feeds install -a 3.配置 make menuconfig 4.编译 make -j ...
- How do I update a GitHub forked repository?
I recently forked a project and applied several fixes. I then created a pull request which was then ...
- opencv-python 学习初探2 去图片水印
我要去除的水印是红色的.网上已经有很不错的帖子,我就不贴自己的代码了.留个指路自己以后查资料. 大概的思路就是用颜色拾取工具,拾取水印颜色,分析了图片,找到规律.比如我的图片水印是红色的,红色差不多g ...
- Spring security框架原理
转自: http://www.blogjava.net/youxia/archive/2008/12/07/244883.html 在SpringSide 3的官方文档中,说安全框架使用的是Spri ...
- div滚动条时div内容显示一半
本文为博主原创,未经允许不得转载 今天在做页面浏览器适配时,将页面中的一个div进行放大时,出现了滚动条,但滚动条对应div中的 内容只能显示一半. 仔细对应属性样式时,div具有overflow:h ...
- YOLO(Darknet官方)训练分类器
目录 1. 分类数据准备 2. Darknet配置 3. Darknet命令使用 4. cifar-10 使用示例 1. 分类数据准备 需要的文件列表: 1. train.list : 训练的图片的绝 ...
- 【Android实验】 UI设计-ListView
目录 实验目的 实验要求 实验内容 实现效果 实验代码 实验总结 实验目的 学习使用ListView 学习使用menu 实验要求 实现一个列表,其中显示班级学号姓名,提供添加功能,如需要删去某一项,长 ...
- python 元组元素计数
#create a tuple tuplex = , , , , , , , , print(tuplex) #return the number of times it appears in the ...
- spring与shiro的集成
web.xml中的配置: <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext. ...
- [设计模式][C++]单例模式
参考:http://blog.csdn.net/hackbuteer1/article/details/7460019 单例模式意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有 ...