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=blue Process finished with exit code 0
Node.js之HTPP URL的更多相关文章
- Node.js中的URL
Node.js中的URL 什么是URL URL是Uniform Location Resource的缩写,翻译为"统一资源定位符",也就是描述资源位置的固定表示方法.被URL描述的 ...
- node.js中的url.parse方法使用说明
node.js中的url.parse方法使用说明:https://blog.csdn.net/swimming_in_it_/article/details/77439975 版权声明:本文为博主原创 ...
- node.js (01http 模块 url 模块)
// 引入 http 模块-->Node.js 中的很多功能都是通过模块实现. var http = require('http'); // http.createServer() 方法创建服务 ...
- Node.js superagent 采集 URL 编码问题
今天在用Node学习采集的时候遇到一个问题,如这个链接地址 http://www.meishij.net/胡萝卜 就是用浏览器的方式访问链接可以打开,但用superagent 去模拟请求,就请求不到 ...
- node.js request请求url错误:证书已过期 Error: certificate has expired
场景: node:8.9.3版本 报错代码: Error: certificate has expired at TLSSocket.<anonymous> (_tls_wrap.js:1 ...
- node.js 模块之url和querystring模块
关系如下: url.parse(string).query | url.parse(string).pathname | | | | | ------ ------------------- http ...
- 极简 Node.js 入门 - 5.2 url & querystring
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- 初学node.js有感一
Node.js感悟 一.前言 很久以前就对node.js十分的好奇和感兴趣,因为种种原因没能去深入的认识了解和学习掌握这门技术,最近正好要做一些项目,其中就用到了node.js中的一些东西,所以借着使 ...
- node.js小案例_留言板
一.前言 通过这个案例复习: 1.node.js中模板引擎的使用 2.node.js中的页面跳转和重定向 二.主要内容 1.案列演示: 2.案列源码:https://github.com/45612 ...
随机推荐
- 【加密算法】SHA
一.简介 安全散列算法(英语:Secure Hash Algorithm,缩写为SHA)是一个密码散列函数家族,是FIPS所认证的安全散列算法.能计算出一个数字消息所对应到的,长度固定的字符串(又称消 ...
- 使用ABP框架踩过的坑系列3
从架构角度来讲,ApplicationService究竟应该如何定位,一种说法是直接对应用例UseCase, 也就是直接对应UI, 这个UI是广义的,不仅仅是浏览器的页面,也包括API调用.还是从我曾 ...
- npm安装和Vue运行
一.开始: 下载地址:http://nodejs.cn/download/ 下载安装: 直到 二.打开CMD,检查是否正常 在安装目录里新增两个文件夹 然后运行命令:如下图: npm config s ...
- 博客迁址 xpeng.scorpionstudio.com
这里不再更新!现在博客的正式地址是: http://xpeng.scorpionstudio.com
- Python实现汉诺塔问题的可视化(以动画的形式展示移动过程)
学习Python已经有一段时间了,也学习了递归的方法,而能够实践该方法的当然就是汉诺塔问题了,但是这次我们不只是要完成对汉诺塔过程的计算,还要通过turtle库来体现汉诺塔中每一层移动的过程. 一.设 ...
- Flask系列02--Flask中的request
一.Flask中的request方法 1.数据相关 #flask中request,render_template等方法要通过引包的方式引入 from flask import request re ...
- numpy 数组相减
a与b的每一列相减
- 873. Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- canvas 绘制圆形进度条
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- cas单点登陆系统-casServer搭建
最近工作比较忙,空闲的时间在搞单点登陆系统,自己写了一套SSO在GitHub上,过程走通了.通过这个例子,自己熟悉了流程,而且破天荒的使用了抽象设计模式,并且熟悉了cookies和session的使用 ...