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学习笔记之Stream模块
Jan 13 NodeJs
NodeJS学习笔记之FS文件模块
Jan 13 NodeJs
nodejs实例解析(输出hello world)
Jan 03 NodeJs
nodejs实现大文件(在线视频)的读取
Oct 16 NodeJs
nodejs+mongodb aggregate级联查询操作示例
Mar 17 NodeJs
nodejs中密码加密处理操作详解
Mar 20 NodeJs
通过nodejs 服务器读取HTML文件渲染到页面的方法
May 17 NodeJs
Nodejs调用Dll模块的方法
Sep 17 NodeJs
NodeJS 将文件夹按照存放路径变成一个对应的JSON的方法
Oct 17 NodeJs
nodeJS进程管理器pm2的使用
Jan 09 NodeJs
nodejs同步调用获取mysql数据时遇到的大坑
Mar 02 NodeJs
详解nodejs 开发企业微信第三方应用入门教程
Mar 12 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中将一段数据存到一个txt文件中并显示其内容
2014/08/15 PHP
PHP代码实现表单数据验证类
2015/07/28 PHP
php 反斜杠处理函数addslashes()和stripslashes()实例详解
2016/12/25 PHP
laravel框架中表单请求类型和CSRF防护实例分析
2019/11/23 PHP
PHP正则之正向预查与反向预查讲解与实例
2020/04/06 PHP
JavaScript学习小结(一)——JavaScript入门基础
2015/09/02 Javascript
js格式化输入框内金额、银行卡号
2016/02/01 Javascript
JS表格组件BootstrapTable行内编辑解决方案x-editable
2016/09/01 Javascript
bootstrap vue.js实现tab效果
2017/02/07 Javascript
js Canvas绘制圆形时钟效果
2017/02/17 Javascript
Node.js学习教程之HTTP/2服务器推送【译】
2017/10/31 Javascript
Angular动态绑定样式及改变UI框架样式的方法小结
2018/09/03 Javascript
微信小程序拍照和摄像功能实现方法示例
2019/02/01 Javascript
node.js中ws模块创建服务端和客户端,网页WebSocket客户端
2019/03/06 Javascript
小程序实现短信登录倒计时
2019/07/12 Javascript
Node.JS发送http请求批量检查文件中的网页地址、服务是否有效可用
2019/11/20 Javascript
JS数组方法concat()用法实例分析
2020/01/18 Javascript
JS sort方法基于数组对象属性值排序
2020/07/10 Javascript
基于vue hash模式微信分享#号的解决
2020/09/07 Javascript
[42:04]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#3Secret VS OG第一局
2016/03/03 DOTA
[40:56]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 Liquid vs TNC
2018/04/01 DOTA
Python中for循环详解
2014/01/17 Python
分析在Python中何种情况下需要使用断言
2015/04/01 Python
Python中操作文件之write()方法的使用教程
2015/05/25 Python
Python中__init__.py文件的作用详解
2016/09/18 Python
Python字典的核心底层原理讲解
2019/01/24 Python
详解Python装饰器
2019/03/25 Python
jupyter notebook中美观显示矩阵实例
2020/04/17 Python
Python可以用来做什么
2020/11/23 Python
美国在线珠宝商店:SZUL
2017/02/11 全球购物
澳大利亚工具仓库:Tools Warehouse
2018/10/15 全球购物
小学生新年寄语
2014/04/03 职场文书
预备党员自我批评思想汇报
2014/10/10 职场文书
党员教师群众路线个人整改措施
2014/10/28 职场文书
导游词之江南园林狮子林
2019/09/16 职场文书
PyCharm配置KBEngine快速处理代码提示冲突、配置命令问题
2021/04/03 Python