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与Mysql的交互示例代码
Aug 18 NodeJs
使用upstart把nodejs应用封装为系统服务实例
Jun 01 NodeJs
Nodejs学习笔记之入门篇
Apr 16 NodeJs
nodejs简单实现中英文翻译
May 04 NodeJs
nodejs加密Crypto的实例代码
Jul 07 NodeJs
nodejs连接mongodb数据库实现增删改查
Dec 01 NodeJs
详解nodejs微信公众号开发——3.封装消息响应模块
Apr 10 NodeJs
NodeJS实现微信公众号关注后自动回复功能
May 31 NodeJs
详解nodejs模板引擎制作
Jun 14 NodeJs
nodejs aes 加解密实例
Oct 10 NodeJs
详解nodejs 配置文件处理方案
Jan 02 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语法(4)
2006/10/09 PHP
菜鸟学PHP之Smarty入门
2007/01/04 PHP
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
2007/09/30 PHP
10条PHP编程习惯助你找工作
2008/09/29 PHP
php返回当前日期或者指定日期是周几
2015/05/21 PHP
ThinkPHP框架安全实现分析
2016/03/14 PHP
写了10年的Javascript也未必全了解的连续赋值运算
2011/03/25 Javascript
Ajax分页插件Pagination从前台jQuery到后端java总结
2016/07/22 Javascript
javascript实现简单的ajax封装示例
2016/12/28 Javascript
Vue项目分环境打包的实现步骤
2018/04/02 Javascript
Angular5给组件本身的标签添加样式class的方法
2018/04/07 Javascript
JavaScript设计模式之缓存代理模式原理与简单用法示例
2018/08/07 Javascript
Postman的下载及安装教程详解
2018/10/16 Javascript
Vue基本使用之对象提供的属性功能
2019/04/30 Javascript
微信小程序学习总结(五)常见问题实例小结
2020/06/04 Javascript
vue+elementui通用弹窗的实现(新增+编辑)
2021/01/07 Vue.js
[01:34]2014DOTA2展望TI 剑指西雅图VG战队专访
2014/06/30 DOTA
python返回昨天日期的方法
2015/05/13 Python
教大家使用Python SqlAlchemy
2016/02/12 Python
实现python版本的按任意键继续/退出
2016/09/26 Python
python数值基础知识浅析
2019/11/19 Python
Python Pandas数据分析工具用法实例
2020/11/05 Python
python实现简单的井字棋游戏(gui界面)
2021/01/22 Python
python 三种方法提取pdf中的图片
2021/02/07 Python
css3 图片圆形显示 如何CSS将正方形图片显示为圆形图片布局
2014/10/10 HTML / CSS
美国知名的时尚购物网站:Anthropologie
2016/12/22 全球购物
怎样写留学自荐信
2013/11/11 职场文书
保险公司晨会主持词
2014/03/22 职场文书
总经理任命书范本
2014/06/05 职场文书
公司自我介绍演讲稿
2014/08/21 职场文书
敬老月活动总结
2014/08/28 职场文书
2015年党风廉政承诺书
2015/01/22 职场文书
小学秋季运动会通讯稿
2015/11/25 职场文书
mysql优化
2021/04/06 MySQL
TV动画《政宗君的复仇》第二季制作决定PV公布
2022/04/02 日漫
Python使用socket去实现TCP客户端和TCP服务端
2022/04/12 Python