对于不同的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笔记2——请求路由的更多相关文章

  1. 前端笔记之NodeJS(二)路由&REPL&模块系统&npm

    一.路由机制(静态资源文件处理) 1.1 Nodejs没有根目录 MIME类型:http://www.w3school.com.cn/media/media_mimeref.asp 在Apache中, ...

  2. Nodejs笔记(二)

    Nodejs事件 Node.js 所有的异步I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一 ...

  3. Symfony2 学习笔记之系统路由

    mfony2 学习笔记之系统路由   漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...

  4. Django:学习笔记(4)——请求与响应

    Django:学习笔记(4)——请求与响应 0.URL路由基础 Web应用中,用户通过不同URL链接访问我们提供的服务,其中首先经过的是一个URL调度器,它类似于SpringBoot中的前端控制器. ...

  5. CSF 中的应用程序请求路由

    编辑人员注释:本文章由 AzureCAT 团队的 Christain Maritnez 撰写. 应用程序请求路由(简称为 ARR)可能是 Microsoft 使用的技术中讨论得最少但极为重要的技术之一 ...

  6. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  7. express搭建后端请求路由,前端进行访问对应的接口 后端解决跨域

    代码在 ==>E:\nodes实战\myserve\testserve 1 express搭建后端请求路由,前端进行访问对应的接口 1) 创建项目目录 express 项目名 -e 然后按照提示 ...

  8. nodejs接收post请求参数

    原文 https://blog.csdn.net/u013263917/article/details/78682270#1.2 nodejs接收post请求参数1.1-浏览器发送post请求参数的方 ...

  9. 技术基础 | 改进版的Apache Cassandra客户端请求路由

    最近我们在客户端的驱动程序中引入了一些变更,这些变更会影响传入的请求在Apache Cassandra集群内的分发方式.   新的默认负载均衡算法即将随驱动程序推出,这些算法将有助于缩短长尾延迟,并提 ...

随机推荐

  1. 【微信公众号】使用a标签打开链接显示空白

    window.location.href 改成 top.location.href

  2. java内存映射文件

    内存映射文件能够让我们创建和修改大文件(大到内存无法读入得文件),对于内存映射文件,我们可以认为是文件已经全部被读入到内存当中,然后当成一个大的数字来访问,简化修改文件的代码. 1.directBuf ...

  3. MSSQL随机数概率测试

    随机概率测试 创建一个表统计create table t_test(ip char(15)) --truncate table t_test; declare @i int ;set @i=0; -- ...

  4. mysql 本机root密码忘记

    1.找到对应的my.conf,在mysqld节点添加:skip-grant-tables  2.重启mysql 即可无密登录 3.update user表中的密码后,去除skip-grant-tabl ...

  5. RAW模板命名规范

    国有国法,家有家规,任何一种开发都要有自己规范,RAW模板也一样,这个文章来介绍一下RAW模板命名的规范. 格式: 开发者或组织_描述词_名称 开发者或组织:如know或自己的组织名 描述词:c-&g ...

  6. isinstance 和 issubclass

    一.isinstance Python 中的isinstance函数 isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数o ...

  7. LATEX使用之字体颜色深浅不一

    今天用Ctex写论文,发现出来的pdf在屏幕上会出现字体颜色深浅不一的现象. google一下之后,在饮水思源bbs上找到了解决方法,用latex+dvitopdf来编译就不会有这个现象了. 另外,对 ...

  8. FPGA工程中用C语言对文件进行处理_生成mif文件

    本博客中有用verilog处理文件数据的代码,本博文采用C 处理文件中的数据. 有时候要生成一个mif文件—— altera memory  initial file.本次工程中我得到的是一个大型的数 ...

  9. JS 寻找孩子并打印路径

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. android点滴之PendingIntent的使用

    一概念 PendingIntent就是一个能够在满足一定条件下运行的Intent,它相比于Intent的优势在于自己携带有Context对象.这样他就不必依赖于某个activity才干够存在. 它和I ...