nodejs之请求路由概述

本文深入探讨了Node.js中路由与请求处理的机制,包括解析URL、使用依赖注入添加路由模块、创建请求处理程序及其与路由模块的连接方式。通过示例展示了如何灵活配置路由,以响应不同URL请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通常来说对于不同的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;

   

您可能感兴趣的文章:
  • java基于包结构的请求路由实现实例分享
  • Nodejs sublime text 3安装与配置
  • nodejs文件操作模块FS(File System)常用函数简明总结
  • nodejs获取本机内网和外网ip地址的实现代码
  • 跟我学Nodejs(三)--- Node.js模块
  • 跟我学Nodejs(二)--- Node.js事件模块
  • 跟我学Nodejs(一)--- Node.js简介及安装开发环境
  • Nodejs使用mysql模块之获得更新和删除影响的行数的方法
  • 利用NodeJS和PhantomJS抓取网站页面信息以及网站截图
  • nodejs中exports与module.exports的区别详细介绍
  • windows系统下简单nodejs安装及环境配置
  • nodejs入门详解(多篇文章结合)

    QQ空间 新浪微博 腾讯微博 搜狐微博 人人网 开心网 百度搜藏更多

    Tags:nodejs 请求路由

    复制链接收藏本文打印本文关闭本文返回首页

    上一篇:node.js实现逐行读取文件内容的代码

    下一篇:下面没有链接了

   

相关文章

  • 2014-06-06Ubuntu中搭建Nodejs开发环境过程分享
  • 2014-06-06使用Node.js实现一个简单的FastCGI服务器实例
  • 2014-06-06nodejs分页类代码分享
  • 2014-06-06什么是Node.js?Node.js详细介绍
  • 2014-06-06nodejs文件操作模块FS(File System)常用函数简明总结
  • 2014-06-06connect中间件session、cookie的使用方法分享
  • 2014-06-06在Node.js中实现文件复制的方法和实例
  • 2014-06-06node.js学习总结之调式代码的方法
  • 2014-06-06使用GruntJS构建Web程序之Tasks(任务)篇
  • 2014-06-06Node.js中require的工作原理浅析

   

文章评论

   

最 近 更 新

   

  • Ubuntu中搭建Nodejs开发环境过程分享
  • 什么是Node.js?Node.js详细介绍
  • node.js实现逐行读取文件内容的代码
  • 使用forever管理nodejs应用教程
  • nodejs npm包管理的配置方法及常用命令介
  • nodejs文件操作模块FS(File System)常用
  • node.js WEB开发中图片验证码的实现方法
  • NODE.JS加密模块CRYPTO常用方法介绍
  • node.js学习总结之调式代码的方法
  • Node.js实现简单聊天服务器

   

热 点 排 行

   

  • nodejs文件操作模块FS(File Sys
  • Nodejs sublime text 3安装与配置
  • nodejs获取本机内网和外网ip地址
  • node.js WEB开发中图片验证码的实
  • NODE.JS加密模块CRYPTO常用方法介
  • Node.js(安装,启动,测试)
  • node.js应用后台守护进程管理器F
  • nodejs npm install全局安装和本
  • node.js实现多图片上传实例
  • node.js实现逐行读取文件内容的代
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值