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实现PHP的print_r函数代码
Mar 14 NodeJs
Nodejs学习笔记之Global Objects全局对象
Jan 13 NodeJs
使用nodejs开发cli项目实例
Jun 03 NodeJs
NodeJs的优势和适合开发的程序
Aug 14 NodeJs
nodeJs链接Mysql做增删改查的简单操作
Feb 04 NodeJs
详解nodejs中的process进程
Mar 19 NodeJs
详解NODEJS基于FFMPEG视频推流测试
Nov 17 NodeJs
Nodejs中怎么实现函数的串行执行
Mar 02 NodeJs
NodeJs实现简易WEB上传下载服务器
Aug 10 NodeJs
nodejs使用node-xlsx生成excel的方法示例
Aug 22 NodeJs
nodejs一个简单的文件服务器的创建方法
Sep 13 NodeJs
nodejs中内置模块fs,path常见的用法说明
Nov 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
ThinkPHP中的create方法与自动令牌验证实例教程
2014/08/22 PHP
PHP精确到毫秒秒杀倒计时实例详解
2019/03/14 PHP
mouse_on_title.js
2006/08/25 Javascript
用js脚本控制asp.net下treeview的NodeCheck的实现代码
2010/03/02 Javascript
javascript一些实用技巧小结
2011/03/18 Javascript
IE8中使用javascript动态加载CSS的解决方法
2014/06/17 Javascript
jquery的clone方法应用于textarea和select的bug修复
2014/06/26 Javascript
javascript中的this详解
2014/12/08 Javascript
jquery实现具有嵌套功能的选项卡
2016/02/12 Javascript
D3.js实现雷达图的方法详解
2016/09/22 Javascript
浅谈jQuery绑定事件会叠加的解决方法和心得总结
2016/10/26 Javascript
js实现砖头在页面拖拉效果
2020/11/20 Javascript
AngularJS+bootstrap实现动态选择商品功能示例
2017/05/17 Javascript
koa2的中间件功能及应用示例
2020/03/05 Javascript
uin-app+mockjs实现本地数据模拟
2020/08/26 Javascript
js实现淘宝浏览商品放大镜功能
2020/10/28 Javascript
javascript实现搜索筛选功能实例代码
2020/11/12 Javascript
python爬虫爬取快手视频多线程下载功能
2018/02/28 Python
Django中反向生成models.py的实例讲解
2018/05/30 Python
python发送告警邮件脚本
2018/09/17 Python
PyCharm导入python项目并配置虚拟环境的教程详解
2019/10/13 Python
Python语法之精妙的十个知识点(装B语法)
2020/01/18 Python
Django vue前后端分离整合过程解析
2020/11/20 Python
浅谈cookie和localStorage那些事
2019/08/27 HTML / CSS
详解如何获取localStorage最大存储大小的方法
2020/05/21 HTML / CSS
Ben Sherman官方网站:英国男装品牌
2019/10/22 全球购物
C语言变量的命名规则都有哪些
2013/12/27 面试题
JAVA和C++区别都有哪些
2015/03/30 面试题
优秀幼教自荐信
2014/02/03 职场文书
师恩难忘教学反思
2014/04/27 职场文书
关于建议书的格式范文
2014/05/20 职场文书
2014年学生会生活部工作总结
2014/11/07 职场文书
三年级学生评语大全
2014/12/26 职场文书
2015年党务公开工作总结
2015/05/19 职场文书
企业安全隐患排查治理制度
2015/08/05 职场文书
python的netCDF4批量处理NC格式文件的操作方法
2022/03/21 Python