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 ...
随机推荐
- web中浏览PDF文件
1.在web中浏览pdf文件. 2.支持大多数主流浏览器,包括IE8 3.参考网址: https://pdfobject.com/ http://mozilla.github.io/pdf.js/ & ...
- CentOS 7 安装并配置 MySQL 5.6
Linux使用MySQL Yum存储库上安装MySQL 5.6,适用于Oracle Linux,Red Hat Enterprise Linux和CentOS系统. 1.添加MySQL Yum存储库 ...
- 用 vs 2017创建 windows 服务
转载自:http://www.cnblogs.com/xujie/p/5695673.html 1.新建windows服务项目,我这里选择的是Framework4.0,没有选择高版本是为了防止在服务在 ...
- s11 day104 数据库表结构与立即支付流程
数据库表结构: 13张 1. 课程大类 2.课程子类 3.学位课程 4.老师表 5.奖学金 6.专题课 7.课程详情 8.课程大纲 9.常见问题 10.章节 11.课时 12.作业表 13.价格策略 ...
- LiLicense server OR Activation code
JET BRAINS系列工具下载地址: https://www.jetbrains.com/products.html?fromMenu License server 输入下列两个任何一个点击 Act ...
- 纯文本-FileOutputStream的解码方式
1.通过string.getBytes(charsetNane)获得的字节数组,字节数组的编码方式,决定了FileOutStream写出文件的格式 例1:字节数组采用“GBK”编码,write生成的文 ...
- Mac OSX sublime text2 各种快捷键
Mac 快捷键 https://support.apple.com/zh-cn/HT201236 Preferences -> Key Bindings - User [ { "key ...
- Ceres入门笔记
介绍 Ceres可以解决下列形式的边界约束鲁棒非线性最小二乘问题 (1) $\min\limits_{x}\quad \frac{1}{2} \sum\limits_{i}\rho_{i}\left( ...
- webpack快速入门——配置JS压缩,打包
1 .首先在webpack.config.js中引入 const uglify = require('uglifyjs-webpack-plugin'); 2.然后在plugins配置里 plugin ...
- js之global 对象 方法
global 作为js的全局对象,但其是无法直接访问的,但是在浏览器中浏览器是将这个对象当做是window对象的一部分,即Date 等Global的属性使用window.Date 可访问到 1.url ...