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连接MongoDB数据库时报错的快速解决方法
May 13 NodeJs
浅谈Nodejs中的作用域问题
Dec 26 NodeJs
NodeJS实现客户端js加密
Jan 09 NodeJs
nodejs爬虫遇到的乱码问题汇总
Apr 07 NodeJs
NodeJS设计模式总结【单例模式,适配器模式,装饰模式,观察者模式】
Sep 06 NodeJs
nodejs+mongodb aggregate级联查询操作示例
Mar 17 NodeJs
nodejs前端模板引擎swig入门详解
May 15 NodeJs
nodejs更新package.json中的dependencies依赖到最新版本的方法
Oct 10 NodeJs
nodejs中函数的调用实例详解
Oct 31 NodeJs
NodeJS模块与ES6模块系统语法及注意点详解
Jan 04 NodeJs
Nodejs + sequelize 实现增删改查操作
Nov 07 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
外媒评选出10支2020年最受欢迎的Dota2战队
2021/03/05 DOTA
PHP程序员最常犯的11个MySQL错误小结
2010/11/20 PHP
php读取mysql中文数据出现乱码的解决方法
2013/08/16 PHP
PHP捕获Fatal error错误的方法
2014/06/11 PHP
PHP 双链表(SplDoublyLinkedList)简介和使用实例
2015/05/12 PHP
JavaScript.The.Good.Parts阅读笔记(一)假值与===运算符
2010/11/16 Javascript
js中的前绑定和后绑定详解
2013/08/01 Javascript
jQuery回车实现登录简单实现
2013/08/20 Javascript
JS 有趣的eval优化输入验证实例代码
2013/09/22 Javascript
面向对象设计模式的核心法则
2013/11/10 Javascript
jquery动态添加option示例
2013/12/30 Javascript
vue.js实现请求数据的方法示例
2017/02/07 Javascript
JavaScript实现旋转轮播图
2020/08/18 Javascript
JS路由跳转的简单实现代码
2017/09/21 Javascript
vue+swiper实现组件化开发的实例代码
2017/10/26 Javascript
webpack4 入门最简单的例子介绍
2018/09/05 Javascript
JavaScript this绑定过程深入详解
2018/12/07 Javascript
python实现的一个火车票转让信息采集器
2014/07/09 Python
python处理文本文件并生成指定格式的文件
2014/07/31 Python
python+selenium实现京东自动登录及秒杀功能
2017/11/18 Python
详解flask入门模板引擎
2018/07/18 Python
python去掉 unicode 字符串前面的u方法
2018/10/21 Python
用python3教你任意Html主内容提取功能
2018/11/05 Python
Python元组常见操作示例
2019/02/19 Python
Django利用cookie保存用户登录信息的简单实现方法
2019/05/27 Python
python爬虫-模拟微博登录功能
2019/09/12 Python
Pytorch通过保存为ONNX模型转TensorRT5的实现
2020/05/25 Python
python实现学生管理系统开发
2020/07/24 Python
python3获取控制台输入的数据的具体实例
2020/08/16 Python
Python3使用Selenium获取session和token方法详解
2021/02/16 Python
JACK & JONES瑞典官方网站:杰克琼斯欧式风格男装
2017/12/23 全球购物
工厂厂长的职责
2013/12/12 职场文书
英语商务邀请函范文
2014/01/16 职场文书
反四风个人对照检查材料
2014/09/26 职场文书
2015年学生资助工作总结
2015/05/25 职场文书
Redis特殊数据类型Geospatial地理空间
2022/06/01 Redis