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 ...
随机推荐
- js框操作-----Selenium快速入门(八)
js框,就是JavaScript中的警告框(alert),确认框(confirm),提示框(prompt),他们都是模态窗口.什么是模态窗口,大家可以自行百度一下,简单说就是弹出的窗口是在最顶端的,你 ...
- NetCore入门篇:(三)Net Core项目Nuget及Bower包管理
一.创建项目 1.如何创建项目,参照上一篇文章 二.程序包介绍 1.Net Core的程序包分前后端两种,后端用nuget,前端用bower. 2.与Net 不同,Net Core引用nuget包时, ...
- SHT20 IIC 寄存器概述
** 注:部分内容来自SHT20芯片手册翻译 ** 1.寄存器列表 名称 指令(bin) 寄存器内容(hex) 主机模式(Trigger T measurement hold master) 1110 ...
- .net core Memcached使用
首先,你要在你电脑上安装配置好Memcached环境哦 Startup类的ConfigureServices中加入 //memcachedcore1 services.AddEnyimMemcache ...
- .Net常用正则判断方法
/// <summary> /// 判断string类型否为数字 /// </summary> /// <param name="strNumber" ...
- 【cocos2d-x 手游研发----精灵的八面玲珑】
继续上一篇文章继续聊吧,这章内容会比较多,也会附上代码,很多朋友加了群,大家在群里面探讨了很多东西,这让大家都觉得受益匪浅,这便是极好的,废话不多了,精灵是游戏的重要组成部分,那ARPG里面的精灵必然 ...
- 廖雪峰Python学习笔记——序列化
序列化 定义:程序运行时所有变量都存在内存中,把变量从内存中变成可存储或可传输的过程称为序列化pickling,在其他语言中称为serialization,marshalling,flattening ...
- 深入学习c++--左值引用和右值引用
#include <iostream> #include <string> #include <vector> using namespace std; int m ...
- dataframe.isnull()函数, DatetimeIndex,黄包车代码155行
待解决 feature 4 完全没懂 feature 5 219行: if (row.userid == action['userid'][row.max_index - 1]): ????
- JVM概念总结:数据类型、堆与栈
Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身: 引用类型的变量保存引用值,引用值代表了某个对象的引用而不是对象的本身,对象的本身存放 ...