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的模块写法入门(实例代码)
Mar 07 NodeJs
将nodejs打包工具整合到鼠标右键的方法
May 11 NodeJs
利用NodeJS的子进程(child_process)调用系统命令的方法分享
Jun 05 NodeJs
nodejs获取本机内网和外网ip地址的实现代码
Jun 01 NodeJs
nodejs事件的监听与触发的理解分析
Feb 12 NodeJs
解决nodejs中使用http请求返回值为html时乱码的问题
Feb 18 NodeJs
简单好用的nodejs 爬虫框架分享
Mar 26 NodeJs
docker中编译nodejs并使用nginx启动
Jun 23 NodeJs
nodejs Assert中equal(),strictEqual(),deepEqual(),strictDeepEqual()比较
Sep 18 NodeJs
nodejs多版本管理总结
Apr 03 NodeJs
详解Nodejs mongoose
Jun 10 NodeJs
nodejs基础之常用工具模块util用法分析
Dec 26 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 正则匹配函数体
2009/08/25 PHP
php将字符串转化成date存入数据库的两种方式
2014/04/28 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
2017/09/13 PHP
PHP下用Swoole实现Actor并发模型的方法
2019/06/12 PHP
thinkphp框架无限级栏目的排序功能实现方法示例
2020/03/29 PHP
常用简易JavaScript函数
2009/04/09 Javascript
jquery子元素过滤选择器使用示例
2013/06/24 Javascript
jQuery超酷平面式时钟效果代码分享
2020/03/30 Javascript
js实现无限级树形导航列表效果代码
2015/09/23 Javascript
JavaScript DOM 对象深入了解
2016/07/20 Javascript
使用Browserify来实现CommonJS的浏览器加载方法
2017/05/14 Javascript
Vuejs中使用markdown服务器端渲染的示例
2017/11/22 Javascript
浅谈Koa2框架利用CORS完成跨域ajax请求
2018/03/06 Javascript
jQuery 实现批量提交表格多行数据的方法
2018/08/09 jQuery
一行JavaScript代码如何实现瀑布流布局
2020/12/11 Javascript
[01:45]典藏宝瓶2+祈求者身心——这就是DOTA2TI9总奖金突破3000万美元的秘密
2019/07/21 DOTA
Python轻量级ORM框架Peewee访问sqlite数据库的方法详解
2017/07/20 Python
Python爬虫将爬取的图片写入world文档的方法
2018/11/07 Python
解决使用PyCharm时无法启动控制台的问题
2019/01/19 Python
Django学习之文件上传与下载
2019/10/06 Python
python手机号前7位归属地爬虫代码实例
2020/03/31 Python
python opencv实现简易画图板
2020/08/27 Python
Tomcat中怎么使用log4j输出所有的log
2016/07/07 面试题
幼儿园保教管理制度
2014/02/03 职场文书
小摄影师教学反思
2014/04/27 职场文书
2014幼儿园教师师德师风演讲稿
2014/09/10 职场文书
假释思想汇报范文
2014/10/11 职场文书
幼儿园中班个人总结
2015/02/28 职场文书
2015年妇联工作总结范文
2015/04/22 职场文书
2015年度物业公司工作总结
2015/04/27 职场文书
2016元旦文艺汇演主持词
2015/07/06 职场文书
培训后的感想
2015/08/07 职场文书
Oracle 触发器trigger使用案例
2022/02/24 Oracle
mysql 生成连续日期及变量赋值
2022/03/20 MySQL
Python实现日志实时监测的示例详解
2022/04/06 Python
MySQL时区造成时差问题
2022/04/13 MySQL