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的子进程(child_process)调用系统命令的方法分享
Jun 05 NodeJs
nodejs中转换URL字符串与查询字符串详解
Nov 26 NodeJs
NodeJS实现阿里大鱼短信通知发送
Jan 17 NodeJs
Nodejs中的this详解
Mar 26 NodeJs
Nodejs下用submit提交表单提示cannot post错误的解决方法
Nov 21 NodeJs
windows系统下更新nodejs版本的方案
Nov 24 NodeJs
nodeJS模块简单用法示例
Apr 21 NodeJs
深入理解NodeJS 多进程和集群
Oct 17 NodeJs
PHPStorm中如何对nodejs项目进行单元测试详解
Feb 28 NodeJs
Nodejs中使用puppeteer控制浏览器中视频播放功能
Aug 26 NodeJs
nodejs制作小爬虫功能示例
Feb 24 NodeJs
Nodejs实现WebSocket代码实例
May 19 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
《魔兽争霸3》重制版究竟重制了什么?玩家:这么糊弄真的好吗?
2020/05/04 魔兽争霸
PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
2014/06/12 PHP
使用PHP Socket 编程模拟Http post和get请求
2014/11/25 PHP
php提交表单发送邮件的方法
2015/03/20 PHP
fromCharCode和charCodeAt 方法
2006/12/27 Javascript
javascript模版引擎-tmpl的bug修复与性能优化分析
2011/10/23 Javascript
Javascript中3种实现继承的方法和代码实例
2014/08/12 Javascript
Nodejs关于gzip/deflate压缩详解
2015/03/04 NodeJs
JQuery中ajax方法访问web服务实例
2015/07/18 Javascript
jQuery实现移动端滑块拖动选择数字效果
2015/12/24 Javascript
使用jQuery监听DOM元素大小变化
2016/02/24 Javascript
谈谈对JavaScript原生拖放的深入理解
2016/09/20 Javascript
Javascript json object 与string 相互转换的简单实现
2016/09/27 Javascript
JS动态添加选项案例分析
2016/10/17 Javascript
JS实现控制图片显示大小的方法【图片等比例缩放功能】
2017/02/18 Javascript
Angular2关于@angular/cli默认端口号配置的问题
2017/07/15 Javascript
解决vue 界面在苹果手机上滑动点击事件等卡顿问题
2018/11/27 Javascript
vue项目中使用bpmn-自定义platter的示例代码
2020/05/11 Javascript
[44:40]2018DOTA2亚洲邀请赛3月30日 小组赛A组Liquid VS OG
2018/03/31 DOTA
二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
2013/12/03 Python
对变量赋值的理解--Pyton中让两个值互换的实现方法
2017/11/29 Python
Python对多属性的重复数据去重实例
2018/04/18 Python
python 将大文件切分为多个小文件的实例
2019/01/14 Python
Python使用线程来接收串口数据的示例
2019/07/02 Python
PyQt5 控件字体样式等设置的实现
2020/05/13 Python
深入理解HTML5定时器requestAnimationFrame的使用
2018/12/12 HTML / CSS
白俄罗斯在线大型超市:e-dostavka.by
2019/07/25 全球购物
给领导的致歉信范文
2014/01/13 职场文书
学习标兵获奖感言
2014/02/20 职场文书
合作协议书怎么写
2014/04/18 职场文书
2016幼儿园中班开学寄语
2015/12/03 职场文书
详解MySQL中的pid与socket
2021/06/15 MySQL
CSS 伪元素::marker详解
2021/06/26 HTML / CSS
Python基于百度API识别并提取图片中文字
2021/06/27 Python
ObjectMapper 如何忽略字段大小写
2021/06/29 Java/Android
MySQL 逻辑备份 into outfile
2022/05/15 MySQL