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中使用monk访问mongodb
Jul 06 NodeJs
在NodeJS中启用ECMAScript 6小结(windos以及Linux)
Jul 15 NodeJs
NodeJS制作爬虫全过程(续)
Dec 22 NodeJs
Nodejs+Socket.io实现通讯实例代码
Feb 13 NodeJs
nodejs中全局变量的实例解析
Mar 07 NodeJs
nodejs入门教程六:express模块用法示例
Apr 24 NodeJs
nodeJS模块简单用法示例
Apr 21 NodeJs
详解Nodejs内存治理
May 13 NodeJs
Mac下通过brew安装指定版本的nodejs教程
May 17 NodeJs
搭建一个nodejs脚手架的方法步骤
Jun 28 NodeJs
nodejs和react实现即时通讯简易聊天室功能
Aug 21 NodeJs
Nodejs中使用puppeteer控制浏览器中视频播放功能
Aug 26 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
AJAX的跨域访问-两种有效的解决方法介绍
2013/06/22 PHP
PHP实现的一致性Hash算法详解【分布式算法】
2018/03/31 PHP
JQuery 操作select标签实现代码
2010/05/14 Javascript
扩展JS Date对象时间格式化功能的小例子
2013/12/02 Javascript
js获取指定日期周数以及星期几的小例子
2014/06/27 Javascript
js识别不同浏览器基于userAgent做判断
2014/07/29 Javascript
JS表的模拟方法
2015/02/05 Javascript
jQuery插件之Tocify动态节点目录菜单生成器附源码下载
2016/01/08 Javascript
js实现密码强度检测【附示例】
2016/03/30 Javascript
jQuery中实现prop()函数控制多选框(全选,反选)
2016/08/19 Javascript
在vue组件中使用axios的方法
2018/03/16 Javascript
ES6 迭代器与可迭代对象的实现
2019/02/11 Javascript
vue19 组建 Vue.extend component、组件模版、动态组件 的实例代码
2019/04/04 Javascript
python pdb调试方法分享
2014/01/21 Python
Python决策树分类算法学习
2017/12/22 Python
python爬虫使用cookie登录详解
2017/12/27 Python
python for 循环获取index索引的方法
2019/02/01 Python
python根据文章标题内容自动生成摘要的实例
2019/02/21 Python
Django对数据库进行添加与更新的例子
2019/07/12 Python
python 进程 进程池 进程间通信实现解析
2019/08/23 Python
Python for循环及基础用法详解
2019/11/08 Python
Python实现打包成库供别的模块调用
2020/07/13 Python
详解HTML5 录音的踩坑之旅
2017/12/26 HTML / CSS
英国现代家具和装饰网站:PN Home
2018/08/16 全球购物
Boden英国官网:英国知名原创时装品牌
2018/11/06 全球购物
西铁城美国官方网站:Citizen Watch美国
2019/11/08 全球购物
介绍一下Linux内核的排队自旋锁
2014/08/27 面试题
小孩百日宴答谢词
2014/01/15 职场文书
给校长的一封建议书
2014/03/12 职场文书
陈欧广告词
2014/03/14 职场文书
高三毕业评语
2014/12/31 职场文书
个人工作违纪检讨书
2015/05/05 职场文书
2015年大学宣传部工作总结
2015/05/26 职场文书
焦裕禄纪念馆观后感
2015/06/09 职场文书
行政复议答复书
2015/07/01 职场文书
新年祝酒词大全
2015/08/11 职场文书