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 提示‘xxx’ 不是内部或外部命令解决方法
Nov 20 NodeJs
windows下安装nodejs及框架express
Aug 07 NodeJs
nodejs redis 发布订阅机制封装实现方法及实例代码
Dec 15 NodeJs
基于Nodejs利用socket.io实现多人聊天室
Feb 22 NodeJs
Nodejs 获取时间加手机标识的32位标识实现代码
Mar 07 NodeJs
nodejs中模块定义实例详解
Mar 18 NodeJs
ubuntu编译nodejs所需的软件并安装
Sep 12 NodeJs
nodejs async异步常用函数总结(推荐)
Nov 17 NodeJs
使用nodejs分离html文件里的js和css详解
Apr 12 NodeJs
NodeJS有难度的面试题(能答对几个)
Oct 09 NodeJs
nodejs如何在package.json中设置多条启动命令
Mar 16 NodeJs
通过实例了解Nodejs模块系统及require机制
Jul 16 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一些错误处理的方法与技巧总结
2013/08/10 PHP
Ajax,UTF-8还是GB2312 eval 还是execScript
2008/11/13 Javascript
window.open以post方式将内容提交到新窗口
2012/12/26 Javascript
jQuery选择器全面总结
2014/01/06 Javascript
下拉框select的绑定示例
2014/09/04 Javascript
微信支付 JS API支付接口详解
2016/07/11 Javascript
AngularJS基础 ng-if 指令用法
2016/08/01 Javascript
JavaScript数组迭代方法
2017/03/03 Javascript
小程序云开发教程如何使用云函数实现点赞功能
2019/05/18 Javascript
详解webpack引用jquery(第三方模块)的三种办法
2019/08/21 jQuery
vue父组件给子组件的组件传值provide inject的方法
2019/10/23 Javascript
vue-cli3项目打包后自动化部署到服务器的方法
2020/09/16 Javascript
如何使用JS console.log()技巧提高工作效率
2020/10/14 Javascript
详解基于element的区间选择组件校验(交易金额)
2021/01/07 Javascript
详解Vite的新体验
2021/02/22 Javascript
[02:21]DOTA2英雄基础教程 蝙蝠骑士
2013/12/16 DOTA
[39:07]LGD vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.21
2018/08/22 DOTA
python中的sort方法使用详解
2014/07/25 Python
Python中将字典转换为XML以及相关的命名空间解析
2015/10/15 Python
qpython3 读取安卓lastpass Cookies
2016/06/19 Python
python操作 hbase 数据的方法
2016/12/18 Python
一个Python最简单的接口自动化框架
2018/01/02 Python
解决python文件双击运行秒退的问题
2019/06/24 Python
Elasticsearch py客户端库安装及使用方法解析
2020/09/14 Python
Smashbox官网:美国知名彩妆品牌
2017/01/05 全球购物
美国最大的在线生存商店:Survival Frog
2020/12/13 全球购物
英国100%防污和防水的靴子:Muck Boot Company
2020/09/08 全球购物
NULL是什么,它是怎么定义的
2015/05/09 面试题
自我介绍演讲稿
2014/01/15 职场文书
党员民主生活会整改措施
2014/09/26 职场文书
销售员岗位职责
2015/02/10 职场文书
2015年护士医德医风自我评价
2015/03/03 职场文书
爱心捐款活动总结
2015/05/09 职场文书
课程设计感想范文
2015/08/11 职场文书
vue中 this.$set的使用详解
2021/11/17 Vue.js
Python必备技巧之字符数据操作详解
2022/03/23 Python