url及node.js url模块随笔
2017-08-14 15:02
246 查看
在将url之前先附上一张图,对url的结构有个清晰的认识
这就是url清晰的组成。
接下来咱们来看一下在前端常常用到的方法
例如
var url = ‘https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=url%E6%96%87%E6%A1%A3‘,
我们想获得ie的值,怎么做呢?
下面是我封装的函数
function html_name(name) {
}
var ie = decodeURI(html_name(‘ie’));
var wd = decodeURI(html_name(‘wd’));
拿来直接调用就行
接下来来说一下在node.js中url常用的几种方法,毕竟node也算是我的一个本命英雄啦!!!!
第一招:化无形为有形
var url = require(“url”);
var burl = “https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=urlbb”
var ourl =url.parse(burl);
获得其各部分的值可以直接调用
如ourl.protocol
ourl.pathname等等
第二招:万剑归宗
var obj= {
protocol: ‘https:’,
slashes: true,
auth: null,
host: ‘example.org:81’,
port: ‘81’,
hostname: ‘example.org’,
hash: null,
search: null,
query: null,
pathname: ‘/foo’,
path: ‘/foo’,
href: ‘https://example.org:81/foo’ };
var tts = url.format(obj);
console.log(tts);
第3招:移花接木
url.resolve(from,to);
如:
url.resolve(“https://example.org:81/foo“,”fou”);
url.resolve(“https://example.org:81/foo/a“,”b”);
url.resolve(“https://example.org:81/foo/a/“,”b”);
打印出来结果分别为:
https://example.org:81/fou
https://example.org:81/foo/b
https://example.org:81/foo/a/b
简而言之,就是将最后一个“/”后面的字符替换。
好吧!真编不下去啦
这就是url清晰的组成。
接下来咱们来看一下在前端常常用到的方法
例如
var url = ‘https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=url%E6%96%87%E6%A1%A3‘,
我们想获得ie的值,怎么做呢?
下面是我封装的函数
function html_name(name) {
html = window.location.search; arr_html = html.split("?"); if(arr_html.length > 1) { arr_html = arr_html[1].split("&"); for(i = 0; i < arr_html.length; i++) { arr_b = arr_html[i].split("="); if(arr_b[0] == name) { return arr_b['1']; break; } } return ''; } else return '';
}
var ie = decodeURI(html_name(‘ie’));
var wd = decodeURI(html_name(‘wd’));
拿来直接调用就行
接下来来说一下在node.js中url常用的几种方法,毕竟node也算是我的一个本命英雄啦!!!!
第一招:化无形为有形
var url = require(“url”);
var burl = “https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=urlbb”
var ourl =url.parse(burl);
获得其各部分的值可以直接调用
如ourl.protocol
ourl.pathname等等
第二招:万剑归宗
var obj= {
protocol: ‘https:’,
slashes: true,
auth: null,
host: ‘example.org:81’,
port: ‘81’,
hostname: ‘example.org’,
hash: null,
search: null,
query: null,
pathname: ‘/foo’,
path: ‘/foo’,
href: ‘https://example.org:81/foo’ };
var tts = url.format(obj);
console.log(tts);
第3招:移花接木
url.resolve(from,to);
如:
url.resolve(“https://example.org:81/foo“,”fou”);
url.resolve(“https://example.org:81/foo/a“,”b”);
url.resolve(“https://example.org:81/foo/a/“,”b”);
打印出来结果分别为:
https://example.org:81/fou
https://example.org:81/foo/b
https://example.org:81/foo/a/b
简而言之,就是将最后一个“/”后面的字符替换。
好吧!真编不下去啦
相关文章推荐
- Nodejs express 获取url参数,post参数的三种方式
- node.js 获取http url路径中的各个参数
- node.js:url
- node.js中的url.resolve方法使用说明
- node.js 获取http url路径中的各个参数 .
- Node.js之HTPP URL
- C# Url操作类封装、仿Node.Js中的Url模块
- Node.js简介url
- Node.js入门小记(三)Api之URL
- node.js中的url.parse方法使用说明
- Node.js中URL的解析
- URL Distribution About NodeJS
- node.js学习笔记--官方原生模块-queryString,url,Buffer
- Node.js简介url
- [Node.js]27. Level 5: URL Building & Doing the Request
- Node.js的URL网址解释的好方法
- node.js之url模块
- node.js 针对不同的请求路径(url) 做出不同的响应
- node.js+android(使用HttpURLConnection和HttpClient)实现文件上传
- NodeJS Connect URL重写中间件组件