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中调用系统命令、Shell脚本和Python脚本的方法和实例
Jan 01 NodeJs
NodeJS创建基础应用并应用模板引擎
Apr 12 NodeJs
nodejs简单实现操作arduino
Sep 25 NodeJs
Nodejs进阶:基于express+multer的文件上传实例
Nov 21 NodeJs
nodejs连接mongodb数据库实现增删改查
Dec 01 NodeJs
基于NodeJS+MongoDB+AngularJS+Bootstrap开发书店案例分析
Jan 12 NodeJs
nodejs模块学习之connect解析
Jul 05 NodeJs
NodeJS 中Stream 的基本使用
Jul 30 NodeJs
nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG的方法
Nov 01 NodeJs
Nodejs实现用户注册功能
Apr 14 NodeJs
详解nodejs内置模块
May 06 NodeJs
NodeJs使用webpack打包项目的方法详解
Feb 28 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初学入门
2006/11/19 PHP
PHP 实现判断用户是否手机访问
2015/01/21 PHP
php的socket编程详解
2016/11/20 PHP
Thinkphp整合微信支付功能
2016/12/14 PHP
php表单文件iframe异步上传实例讲解
2017/07/26 PHP
在laravel中使用with实现动态添加where条件
2019/10/10 PHP
PHP中的输出echo、print、printf、sprintf、print_r和var_dump的示例代码
2020/12/01 PHP
再谈IE中Flash控件的自动激活 ObjectWrap
2007/03/09 Javascript
Javascript 面向对象 继承
2010/05/13 Javascript
jQuery获取地址栏参数插件(模仿C#)
2010/10/26 Javascript
基于jQuery的可用于选项卡及幻灯的切换插件
2011/03/28 Javascript
使用jquery中height()方法获取各种高度大全
2014/04/02 Javascript
jquery实现侧边弹出的垂直导航
2014/12/09 Javascript
如何减少浏览器的reflow和repaint
2015/02/26 Javascript
Java Mybatis框架入门基础教程
2015/09/21 Javascript
详解JavaScript基于面向对象之创建对象(1)
2015/12/10 Javascript
codeMirror插件使用讲解
2017/01/16 Javascript
基于jQuery实现的Ajax 验证用户名唯一性实例代码
2017/06/28 jQuery
强大的JavaScript响应式图表Chartist.js的使用
2017/09/13 Javascript
Vue使用虚拟dom进行渲染view的方法
2019/12/26 Javascript
详解Python的Django框架中manage命令的使用与扩展
2016/04/11 Python
Python 爬虫之超链接 url中含有中文出错及解决办法
2017/08/03 Python
Python使用min、max函数查找二维数据矩阵中最小、最大值的方法
2018/05/15 Python
深入解析Python小白学习【操作列表】
2019/03/23 Python
python实现将文件夹内的每张图片批量分割成多张
2019/07/22 Python
Python读写文件模式和文件对象方法实例详解
2019/09/17 Python
Iconfont(矢量图标)+iconmoon(图标svg互转)配合javascript实现社交分享系统
2020/04/21 Python
AmazeUI 模态窗口的实现代码
2020/08/18 HTML / CSS
新奥尔良珠宝:Mignon Faget
2020/11/23 全球购物
.NET概念性的面试题
2012/02/29 面试题
咖啡厅创业计划书范本
2014/01/22 职场文书
优质服务演讲稿
2014/05/14 职场文书
高中校园广播稿3篇
2014/09/29 职场文书
欢迎家长标语
2014/10/08 职场文书
图文详解nginx日志切割的实现
2022/01/18 Servers
Docker与K8s关系介绍不会Docker也可以使用K8s
2022/06/25 Servers