位置: 编程技术 - 正文
推荐整理分享nodejs之请求路由概述(nodejs 请求),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:nodejs 路由,nodejs url,node.js post请求,nodejs请求http,nodejs获取请求ip,nodejs请求http,nodejs请求http,node 请求,内容如对您有帮助,希望把文章链接给更多的朋友!
通常来说对于不同的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 ' href: The full URL that was originally parsed. Both the protocol and host are lowercased.Example: ' protocol: The request protocol, lowercased.Example: 'http:' host: The full lowercased host portion of the URL, including port information.Example: 'host.com:' 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: '' 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/hquery=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]();的表达式。代码如下所示:
我的Node.js学习之路(一) 一,node.js介绍这些网上一大堆信息,我只精简1,什么是node.js从核心上说:Node.js是个事件驱动的服务器端javascript环境,也就是说,我们可以像使用PHP
我的Node.js学习之路(二)NPM模块管理 NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准。有了NPM,可以很快的找到特定服务要使用的包,进行下载、安装以及管
我的Node.js学习之路(三)--node.js作用、回调、同步和异步代码 以及事件循环 一,node.js的作用,I/O的意义,(I/O是输入/输出的简写,如:键盘敲入文本,输入,屏幕上看到文本显示输出。鼠标移动,在屏幕上看到鼠标的移动。终
标签: nodejs 请求
本文链接地址:https://www.jiuchutong.com/biancheng/374153.html 转载请保留说明!友情链接: 武汉网站建设