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的前后端分离的思考与实践(二)模版探索
Sep 26 NodeJs
nodejs开发环境配置与使用
Nov 17 NodeJs
详谈nodejs异步编程
Dec 04 NodeJs
nodejs中使用多线程编程的方法实例
Mar 24 NodeJs
Nodejs如何搭建Web服务器
Mar 28 NodeJs
async/await与promise(nodejs中的异步操作问题)
Mar 03 NodeJs
nodejs+websocket实时聊天系统改进版
May 18 NodeJs
在Debian(Raspberry Pi)树莓派上安装NodeJS的教程详解
Sep 19 NodeJs
NodeJs实现定时任务的示例代码
Dec 05 NodeJs
nodejs中密码加密处理操作详解
Mar 20 NodeJs
Nodejs中怎么实现函数的串行执行
Mar 02 NodeJs
nodejs和react实现即时通讯简易聊天室功能
Aug 21 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/12/13 PHP
php 什么是PEAR?
2009/03/19 PHP
php语言流程控制中的主动与被动
2012/11/05 PHP
Zend Framework入门知识点小结
2016/03/19 PHP
jquery实现简单的无缝滚动
2015/04/15 Javascript
AngularJS中实现用户访问的身份认证和表单验证功能
2016/04/21 Javascript
解决webpack -p压缩打包react报语法错误的方法
2017/07/03 Javascript
javaScript中的空值和假值
2017/12/18 Javascript
js实现前面自动补全位数的方法
2018/10/10 Javascript
Layui 动态禁止select下拉的例子
2019/09/03 Javascript
vue学习笔记之给组件绑定原生事件操作示例
2020/02/27 Javascript
微信小游戏中three.js离屏画布的示例代码
2020/10/12 Javascript
python+pyqt实现右下角弹出框
2017/10/26 Python
python 读取DICOM头文件的实例
2018/05/07 Python
PyTorch CNN实战之MNIST手写数字识别示例
2018/05/29 Python
浅谈python编译pyc工程--导包问题解决
2019/03/20 Python
pycharm 激活码及使用方式的详细教程
2020/05/12 Python
Python Selenium实现无可视化界面过程解析
2020/08/25 Python
canvas绘制树形结构可视图形的实现
2020/04/03 HTML / CSS
The North Face北面法国官网:美国著名户外品牌
2019/11/01 全球购物
社区志愿者心得体会
2014/01/03 职场文书
《孔子游春》教学反思
2014/02/25 职场文书
歌唱比赛主持词
2014/03/18 职场文书
乡镇网格化管理实施方案
2014/03/23 职场文书
社区戒毒工作方案
2014/06/04 职场文书
同志主要表现材料
2014/08/21 职场文书
旅游活动总结
2014/08/27 职场文书
2014年体育教师工作总结
2014/12/03 职场文书
关于感谢信的范文
2015/01/23 职场文书
演讲开场白台词大全
2015/05/29 职场文书
二十年同学聚会感言
2015/07/30 职场文书
导游词之河北野三坡
2019/12/11 职场文书
SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 WIN-8IVSNAQS8T7\Administrator 的信息
2021/06/30 SQL Server
Spring Boot优化后启动速度快到飞起技巧示例
2022/07/23 Java/Android
windows系统搭建WEB服务器详细教程
2022/08/05 Servers
纯CSS打字动画的实现示例
2022/08/05 HTML / CSS