nodejs笔记2——请求路由
来源:互联网 发布:路由器mac地址 编辑:程序博客网 时间:2024/06/16 09:50
对于不同的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 ofpathname
andsearch
.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笔记2——请求路由
- nodejs之请求路由概述
- nodejs-路由学习笔记
- NodeJs——(12)添加路由
- NodeJs如何处理请求的路由
- nodejs路由--get和post请求
- Nodejs笔记(2)——模块
- nodejs学习笔记(五)——处理get和post请求
- NodeJs——(10)REST风格的路由规则
- nodejs如何请求路由,http和url模块
- nodejs模块:简单http请求路由,仿express
- nodejs 路由
- Nodejs笔记—入门篇
- nodejs 学习笔记---非阻塞请求响应
- nodejs学习笔记一——nodejs安装
- 深入浅出nodejs学习笔记——nodejs简介
- laravel 学习笔记——路由(路由与控制器)
- laravel 学习笔记——路由(路由与控制器)
- 黑马程序员--字符串处理
- 父类与子类之间变量和方法的调用
- Windows 2008 / Windows 7 x64: The ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local
- 回调函数(callback)是什么?
- C#_delegate EndInvoke
- nodejs笔记2——请求路由
- Ext 的国际化
- 人工智能:EMC GP发布Pivotal HD和HAWG详解
- android ADT 无法获取更新列表 解决办法
- media-type,media-query
- python内置类型(list,dictionary, tuple , string, )
- 数据库的一些知识点,select 查询语句复习总结
- 大数据核心技术源码分析之-Avro篇-2
- [week2]每周总结与工作计划