nodejs之请求路由概述


Posted in NodeJs onJuly 05, 2014

通常来说对于不同的URL请求,服务器应该有不同的反应。我们要为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。我们需要的所有数据都会包含在request对象中,该对象作为onRequest()回调函数的第一个参数传递。为了解析这些数据,需要调用额外的模块,分别是url和querystring模块。
 
URL:This
 module has utilities for URL resolution and parsing. Call require('url') to
 use it.
 
Parsed URL objects have some or all of the following fields, depending on whether or not they exist in the URL string. Any parts that are not in the URL string will not be in the parsed object. Examples are shown for the URL
 
'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
 
href: The full URL that was originally parsed. Both the protocol and host are lowercased.
Example: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
 
protocol: The request protocol, lowercased.
Example: 'http:'
 
host: The full lowercased host portion of the URL, including port information.
Example: 'host.com:8080'
 
auth: The authentication information portion of a URL.
Example: 'user:pass'
 
hostname: Just the lowercased hostname portion of the host.
Example: 'host.com'
 
port: The port number portion of the host.
Example: '8080'
 
pathname: The path section of the URL, that comes after the host and before the query, including the initial slash if present.
Example: '/p/a/t/h'
 
search: The 'query string' portion of the URL, including the leading question mark.
Example: '?query=string'
 
path: Concatenation of pathname and search.
Example: '/p/a/t/h?query=string'
 
query: Either the 'params' portion of the query string, or a querystring-parsed object.
Example: 'query=string' or {'query':'string'}
 
hash: The 'fragment' portion of the URL including the pound-sign.
Example: '#hash'
 
我们将使用依赖注入的方式较松散地添加路由模块。作为路由目标的函数称为请求处理程序,请求处理函数的实现需要创建一个叫做requestHandlers的模块,当然也可以命名为其他。并对于每一个请求处理程序,添加一个占位用函数,随后将这些函数作为模块的方法导出,这样就可以将请求处理程序和路由模块连接起来,让路由有路可循。
 
特别指出的是,这里需要将一系列请求处理程序通过一个对象来传递,并且需要使用松耦合的方式将这个对象注入到route()函数中。

我们可以用从关联数组中获取元素一样的方式从传递的对象中获取请求处理函数,因此就有了简洁流畅的形如handle[pathname]();的表达式。代码如下所示:

var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
NodeJs 相关文章推荐
Nodejs极简入门教程(一):模块机制
Oct 25 NodeJs
Nodejs基于LRU算法实现的缓存处理操作示例
Mar 17 NodeJs
详解如何在NodeJS项目中优雅的使用ES6
Apr 22 NodeJs
NodeJS实现图片上传代码(Express)
Jun 30 NodeJs
让nodeJS支持ES6的词法----babel的安装和使用方法
Jul 31 NodeJs
nodejs动态创建二维码的方法
Aug 12 NodeJs
nodejs实现截取上传视频中一帧作为预览图片
Dec 10 NodeJs
nodeJS微信分享
Dec 20 NodeJs
nodejs 如何手动实现服务器
Aug 20 NodeJs
NodeJS有难度的面试题(能答对几个)
Oct 09 NodeJs
NodeJS多种创建WebSocket监听的方式(三种)
Jun 04 NodeJs
Nodejs 数组的队列以及forEach的应用详解
Feb 25 NodeJs
Nodejs中自定义事件实例
Jun 20 #NodeJs
Nodejs sublime text 3安装与配置
Jun 19 #NodeJs
nodejs实现黑名单中间件设计
Jun 17 #NodeJs
nodejs分页类代码分享
Jun 17 #NodeJs
nodejs npm包管理的配置方法及常用命令介绍
Jun 05 #NodeJs
nodejs npm install全局安装和本地安装的区别
Jun 05 #NodeJs
nodejs文件操作模块FS(File System)常用函数简明总结
Jun 05 #NodeJs
You might like
php 魔术函数使用说明
2010/02/21 PHP
让PHP显示Facebook的粉丝数量方法
2014/01/08 PHP
php常见的魔术方法详解
2014/12/25 PHP
基于php实现七牛抓取远程图片
2015/12/01 PHP
yii2中的rules 自定义验证规则详解
2016/04/19 PHP
Laravel使用支付宝进行支付的示例代码
2017/08/16 PHP
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
2018/09/26 PHP
php基于Redis消息队列实现的消息推送的方法
2018/11/28 PHP
在IE模态窗口中自由查看HTML源码的方法
2007/03/08 Javascript
jquery插件制作 表单验证实现代码
2012/08/17 Javascript
jquery 遍历数组 each 方法详解
2016/05/25 Javascript
总结在前端排序中遇到的问题
2016/07/19 Javascript
利用JS提交表单的几种方法和验证(必看篇)
2016/09/17 Javascript
JS+WCF实现进度条实时监测数据加载量的方法详解
2017/12/19 Javascript
详解NodeJs开发微信公众号
2018/05/25 NodeJs
koa大型web项目中使用路由装饰器的方法示例
2019/04/02 Javascript
Vue组件通信的几种实现方法
2019/04/25 Javascript
小程序封装路由文件和路由方法(5种全解析)
2019/05/26 Javascript
layui的select联动实现代码
2019/09/28 Javascript
解决Antd Table组件表头不对齐的问题
2020/10/27 Javascript
Vue看了就会的8个小技巧
2021/01/21 Vue.js
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
Python正则表达式匹配HTML页面编码
2015/04/08 Python
python写入xml文件的方法
2015/05/08 Python
Php多进程实现代码
2018/05/07 Python
对python中的argv和argc使用详解
2018/12/15 Python
python 自动重连wifi windows的方法
2018/12/18 Python
Python3进制之间的转换代码实例
2019/08/24 Python
简单了解Python变量作用域正确使用方法
2020/06/12 Python
美国著名手表网站:Timepiece
2017/11/15 全球购物
住宅质量保证书
2014/04/29 职场文书
计算机科学技术自荐信
2014/06/12 职场文书
2014年劳动部工作总结
2014/12/11 职场文书
离婚协议书范本
2015/01/26 职场文书
博士生专家推荐信
2015/03/25 职场文书
SQL实现LeetCode(176.第二高薪水)
2021/08/04 MySQL