Node.js之HTPP URL
几乎每门编程语言都会包括网络这块,Node.js也不例外。今天主要是熟悉下Node.js中HTTP服务。其实HTTP模块是相当低层次的,它不提供路由、cookie、缓存等,像Web开发中不会直接使用,但还是要熟悉下,这样也方便以后的学习。
一、统一资源标识符URL
这个是非常常见的,在Node.js中有几种处理。
http://user:pass@host.com:80/resource/path/?query=string#hash
协议://身份认证@主机名.com:端口/路径/搜索/查询#散列
在URL模块中可以URL定义的属性和方法
exports.parse = urlParse; exports.resolve = urlResolve; exports.resolveObject = urlResolveObject; exports.format = urlFormat;exports.Url = Url;function Url() {this.protocol = null;this.slashes = null;this.auth = null;this.host = null;this.port = null;this.hostname = null;this.hash = null;this.search = null;this.query = null;this.pathname = null;this.path = null;this.href = null; }
上面代码可以看到URL模块定义了protocol、slashes等这些属性,还有parse、resolve 等方法.
1、URL字符串转URL对象 parse
/*** Created by Administrator on 2016/3/26.*/ var url=require('url'); var urlStr='http://user:pass@host.com:80/rseource/path?query=string#hash'; //parse(urlStr,[parseQueryString],[slashesDenoteHost]) //parseQueryString 布尔值 true:URL查询字符串部分解析为对象字面量默认false //slashesDenoteHost 布尔值 true:把格式为//host/path的URL解析为:{host:'host',pathname:'/path'},而不是{pathname:'//host/path'} 默认false var urlObj=url.parse(urlStr,true,false); console.log(urlObj);
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe URL.js Url {protocol: 'http:',slashes: true,auth: 'user:pass',host: 'host.com:80',port: '80',hostname: 'host.com',hash: '#hash',search: '?query=string',query: { query: 'string' },pathname: '/rseource/path',path: '/rseource/path?query=string',href: 'http://user:pass@host.com:80/rseource/path?query=string#hash' }Process finished with exit code 0
2.URL重定向resolve
有时候请求的url和实际的物理地址并不一样,这就要进行虚拟地址和物理地址的转换。
var url=require('url'); var originalUrl='http://user:pass@host.com:80/rseource/path?query=string#hash'; var newResource='/another/path?querynew'; console.log(url.resolve(originalUrl,newResource));
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe URL.js http://user:pass@host.com:80/another/path?querynew Process finished with exit code 0
3.处理查询字符串和表单参数
在做web开发中常常需要向服务端get 、post请求,请求的时候可能会带一些参数,需要对参数进行处理.比如:查询字符串转js对象或js对象转字符串。
这里要用到queryString模块的parse()和stringify()函数。
var qString=require('querystring') //QueryString.parse = QueryString.decode = function(qs, sep, eq, options) //1.qs 字符串 //2.sep 使用的分隔符 默认& //3.ep 使用的运算符 默认= //4.一个具有maxKey属性的对象 能够限制生成的对象可以包含的键的数量默认1000,0则无限制 var params=qString.parse("name=cuiyanwei&color=red&color=blue"); console.log(params); //QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) console.log(qString.stringify(params,"&","="));
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe URL.js { name: 'cuiyanwei', color: [ 'red', 'blue' ] } name=cuiyanwei&color=red&color=blueProcess finished with exit code 0
转载于:https://www.cnblogs.com/5ishare/p/5323101.html
Node.js之HTPP URL相关推荐
- node.js中的url.parse方法
学习node.js中的url.parse方法 文章目录 前言 一.URL模块之parse方法详解 1.参数 2.实例 例子1:url.parse只传一个参数的情况 例子2:url.parse第二个参数 ...
- Node.js 系列:构建原生 Node.js 应用
原生 Node.js 应用 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境 Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效 Nod ...
- Node.js 多版本安装方式
卸载已有的Node.js 下载nvm:https://github.com/coreybutler/nvm-windows/releases 在电脑中找个磁盘创建两个nvm 和 nodejs目录 并且 ...
- node.js小案例_留言板
一.前言 通过这个案例复习: 1.node.js中模板引擎的使用 2.node.js中的页面跳转和重定向 二.主要内容 1.案列演示: 2.案列源码:https://github.com/456123 ...
- html js不触发_「万字整理 」这里有一份Node.js入门指南和实践,请注意查收 ??
前言 什么是 Node.js 呢 ? JS 是脚本语言,脚本语言都需要一个解析器才能运行.对于写在 HTML 页面里的 JS,浏览器充当了解析器的角色.而对于需要独立运行的 JS,NodeJS 就是一 ...
- JavaScript教程9 - Node.js
Node.js 安装Node.js https://nodejs.org/ npm npm其实是Node.js的包管理工具(package manager). 命令行模式 执行node hello.j ...
- JavaScript之后端Web服务器开发Node.JS基本模块学习篇
JavaScript之后端Web服务器开发Node.JS基本模块学习篇 基本模块 fs文件系统模块 stream支持流模块 http crypto加密模块 基本模块 因为Node.js是运行在服务区端 ...
- node.js 学习笔记三:路由url
一.修改server.js var http = require("http"); var url = require("url"); //导入内置url模块f ...
- puppeteer api_使用Node.js和puppeteer API从URL创建PDF文件
puppeteer api We will continue using Node.js and puppeteer which is a node library. As we saw in our ...
最新文章
- Office 365发送超大附件
- 前端学习(2363):数据缓存
- 威纶通触摸屏与mysql_威纶触摸屏应用实例 以及威纶通触摸屏配方组合
- 素描java字母_进行Java基本GUI设计需要用到的包是
- POJ 2773 Happy 2006 【数论,容斥原理+二分】
- 504 Gateway Time-out 错误处理记录
- 【numpy】数组增加一维(升维)小结
- Python开发环境配置 Vim + Ctags+ TagList
- 易语言解析html实例,易语言解析JSON教程
- MATLAB除法运算的精度问题及其解决方案
- Docker下载与安装(2020)
- 深入理解ext4文件系统
- struts1和struts2的区别
- linux远程关机重启命令,Linux关机命令解析
- 用Nginx在win2008服务器部署ssl后xmlhttp异常(msxml6.dll 错误 ‘80072f7d‘ )的解决方法
- 软件的开发模型之瀑布型和快速原型
- Java实现拼图小游戏(3)—— 添加图片(含JFrame源码阅读)
- win10微软拼音输入法无提示怎么办?
- 自研芯片,快手打响 ToB 第一战!
- Nand Flash 通过调整阈值的Retry方法简单描述
热门文章
- numpy维度交换_“lazy”的transpose()函数——从numpy 数组的内存布局讲起
- vue 分模块打包 脚手架_Vue面试官最爱的底层源码问题,你可以这样回答!
- event php,PHP event 事件机制
- <MySQL>何时使用普通索引,何时使用唯一索引
- getseconds补0_Java Duration类| getSeconds()方法与示例
- Java ObjectStreamField getOffset()方法与示例
- Java PushbackInputStream markSupported()方法与示例
- linux制作成后台服务,把dotnetcore 控制台app设置成linux后台服务
- 20160818_周报日志之二
- 376. 摆动序列 golang