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下打包模块archiver详解
Dec 03 NodeJs
nodejs中操作mysql数据库示例
Dec 20 NodeJs
nodejs导出excel的方法
Jun 30 NodeJs
NodeJs读取JSON文件格式化时的注意事项
Sep 25 NodeJs
基于nodejs 的多页面爬虫实例代码
May 31 NodeJs
ajax +NodeJS 实现图片上传实例
Jun 06 NodeJs
Nodejs进阶之服务端字符编解码和乱码处理
Sep 04 NodeJs
Nodejs调用WebService的示例代码
Sep 29 NodeJs
Mac 安装 nodejs方法(图文详细步骤)
Oct 30 NodeJs
用Electron写个带界面的nodejs爬虫的实现方法
Jan 29 NodeJs
详解nodejs内置模块
May 06 NodeJs
分享五个Node.js开发的优秀实践 
Apr 07 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 Static关键字实用方法
2010/06/04 PHP
PHP容易忘记的知识点分享
2013/04/30 PHP
CI框架扩展系统核心类的方法分析
2016/05/23 PHP
PHP调用QQ互联接口实现QQ登录网站功能示例
2019/10/24 PHP
用js实现下载远程文件并保存在本地的脚本
2008/05/06 Javascript
JQUERY操作JSON实例代码
2010/02/09 Javascript
JS动态增加删除UL节点LI及相关内容示例
2014/05/21 Javascript
jQuery页面加载初始化常用的三种方法
2014/06/04 Javascript
jQuery中:checked选择器用法实例
2015/01/04 Javascript
JQuery.get提交页面不跳转的解决方法
2015/01/13 Javascript
firefox浏览器用jquery.uploadify插件上传时报HTTP 302错误
2015/03/01 Javascript
leaflet的开发入门教程
2016/11/17 Javascript
javascript-解决mongoose数据查询的异步操作
2016/12/22 Javascript
bootstrap laydate日期组件使用详解
2017/01/04 Javascript
JavaScript基本语法_动力节点Java学院整理
2017/06/26 Javascript
js学习总结之dom2级事件基础知识详解
2017/07/27 Javascript
jQuery pager.js 插件动态分页功能实例分析
2019/08/02 jQuery
vue 使用post/get 下载导出文件操作
2020/08/07 Javascript
理解Python中的With语句
2016/03/18 Python
Python新手们容易犯的几个错误总结
2017/04/01 Python
Python使用re模块实现信息筛选的方法
2018/04/29 Python
使用Flask集成bootstrap的方法
2018/07/24 Python
解决pycharm不能自动补全第三方库的函数和属性问题
2020/03/12 Python
python中for in的用法详解
2020/04/17 Python
日本小田急百货官网:Odakyu
2018/07/19 全球购物
Nike香港官网:Nike HK
2019/03/23 全球购物
法国滑雪假期的专家:Ski Planet
2019/11/02 全球购物
武汉世纪畅想数字传播有限公司 .NET笔试题
2015/06/13 面试题
校园安全检查制度
2014/02/03 职场文书
公司行政专员岗位职责
2014/08/24 职场文书
党员先进性教育整改措施
2014/09/18 职场文书
无刑事犯罪记录证明范本
2014/09/29 职场文书
学前班幼儿评语大全
2014/12/29 职场文书
2015年班组工作总结
2015/04/20 职场文书
检讨书之工作不认真
2019/08/14 职场文书
MySQL完整性约束的定义与实例教程
2021/05/30 MySQL