nodejs 路径处理

https://blog.csdn.net/liudongdong19/article/details/81353159

获取 /路径/文件名/扩展名
获取路径:path.dirname(filepath)
获取文件名:path.basename(filename)
获取扩展名:path.extname(filepath)
获取所在路径

const path=require("path");
var filepath='/node/base/path/test.js';
console.log(
    path.dirname(filepath)
)
//输出/node/base/path
获取文件名 
path.basename(filepath)实际上是获取了路径的最后一部分, 
而不是真正的获取到了文件名,但是在实际应用中最后的字符往往就是我们的文件名

const path = require("path");
console.log(path.basename("/node/base/path/test.js"));
//输出    test.js
console.log(path.basename("/node/base/path/test/"));
//输出    test
console.log(path.basename("/node/base/path/test"));
//输出    test
如果只想获取文件名,不需要扩展名则可以使用第二个参数,第二个参数就是指定的文件扩展名

const path = require("path");
console.log(path.basename("/node/base/path/test.js",".js"));
//输出    test
获取文件扩展名

const path = require("path");
console.log(path.extname("/node/base/path/test.js",".js"));
//输出    .js
更详细的规则是如下:(假设 path.basename(filepath) === B )

从B的最后一个.开始截取,直到最后一个字符。
如果B中不存在.,或者B的第一个字符就是.,那么返回空字符串。
下面是官方手册的一个案例

path.extname('index.html')
// Returns: '.html'
 
path.extname('index.coffee.md')
// Returns: '.md'
 
path.extname('index.')
// Returns: '.'
 
path.extname('index')
// Returns: ''
 
path.extname('.index')
// Returns: ''
文件路径分解/组合
path.join([…paths])
path.resolve([…paths])
path.join([…paths]) 
接收多个路劲字符串参数,把路径参数拼接起来然后再执行一下normalize

例子如下

const path=require("path");
path.join("/temp",'node','/js/test.js')
//输出    \temp\node\js\test.js
path.join("/temp",'node','/js/test.js/','..')
//输出    \temp\node\js
path定义的伪代码如下:

module.exports.join = function(){
  var paths = Array.prototye.slice.call(arguments, 0);
  return this.normalize( paths.join('/') );
};
path.resolve([…paths]) 
这个接口的作用就相当于在shell命令下,从左到右运行一遍cd path命令, 
最终获取的绝对路径/文件名,就是这个接口所返回的结果了。

比如

path.resolve(‘/temp/node’, ‘./test’)

可以看成下面命令的结果

cd /temp/node
cd ./test
更多的例子如下
var path = require('path');
 
// 假设当前工作路径是 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path
 
// 输出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path
console.log( path.resolve('') )
 
// 输出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path
console.log( path.resolve('.') )
 
// 输出 /foo/bar/baz
console.log( path.resolve('/foo/bar', './baz') );
 
// 输出 /foo/bar/baz
console.log( path.resolve('/foo/bar', './baz/') );
 
// 输出 /tmp/file
console.log( path.resolve('/foo/bar', '/tmp/file/') );
 
// 输出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path/www/js/mod.js
console.log( path.resolve('www', 'js/upload', '../mod.js') );
路径解析
path.normalize(filepath) 
从官方文档的描述来看,path.normalize(filepath) 应该是比较简单的一个API,不过用起来总是觉得没底。

为什么呢?API说明过于简略了,包括如下:

如果路径为空,返回.,相当于当前的工作路径。
将对路径中重复的路径分隔符(比如linux下的/)合并为一个。
对路径中的. 、..进行处理。(类似于shell里的cd ..)
如果路径最后有/,那么保留该/。
代码示例如下。建议读者把代码拷贝出来运行下,看下实际效果。

var path = require('path');
var filepath = '/tmp/demo/js/test.js';
 
var index = 0;
 
var compare = function(desc, callback){
  console.log('[用例%d]:%s', ++index, desc);
  callback();
  console.log('\n');
};
 
compare('路径为空', function(){
  // 输出 .
  console.log( path.normalize('') );
});
 
compare('路径结尾是否带/', function(){
  // 输出 /tmp/demo/js/upload
  console.log( path.normalize('/tmp/demo/js/upload') );
 
  // /tmp/demo/js/upload/
  console.log( path.normalize('/tmp/demo/js/upload/') );
});
 
compare('重复的/', function(){
  // 输出 /tmp/demo/js
  console.log( path.normalize('/tmp/demo//js') );
});
 
compare('路径带..', function(){
  // 输出 /tmp/demo/js
  console.log( path.normalize('/tmp/demo/js/upload/..') );
});
 
compare('相对路径', function(){
  // 输出 demo/js/upload/
  console.log( path.normalize('./demo/js/upload/') );
 
  // 输出 demo/js/upload/
  console.log( path.normalize('demo/js/upload/') );
});
 
compare('不常用边界', function(){
  // 输出 ..
  console.log( path.normalize('./..') );
 
  // 输出 ..
  console.log( path.normalize('..') );
 
  // 输出 ../
  console.log( path.normalize('../') );
 
  // 输出 /
  console.log( path.normalize('/../') );
 
  // 输出 /
  console.log( path.normalize('/..') );
});
文件路径分解/组合

path.format(pathObject):将pathObject的root、dir、base、name、ext属性,按照一定的规则,组合成一个文件路径。
path.parse(filepath):path.format()方法的反向操作。
我们先来看看官网对相关属性的说明。

首先是linux下

然后是windows下 


path.format(pathObject) 
阅读相关API文档说明后发现,path.format(pathObject)中,pathObject的配置属性是可以进一步精简的。

根据接口的描述来看,以下两者是等价的。

root vs dir:两者可以互相替换,区别在于,路径拼接时,root后不会自动加/,而dir会自动加/。
base vs name+ext:两者可以互相替换。
例子如下

const path = require("path");
//root+base
var p1 = {
    root: "/temp/",
    base: "test.js"
}
path.format(p1)  //输出   /temp/test.js
//dir+name+ext
var p2 = {
    dir: "/temp",
    name: 'test',
    ext: '.js'
}
path.format(p2) //输出    /temp/test.js
path.parse(filepath) 
path.format(pathObject) 的反向操作,也就是把一个路径转换为具有 root/dir/name/ext/base 等属性的对象

以下是官方给出的例子:

path.parse('/home/user/dir/file.txt')
// returns
// {
//    root : "/",
//    dir : "/home/user/dir",
//    base : "file.txt",
//    ext : ".txt",
//    name : "file"
// }
获取相对路径
接口:path.relative(from, to)

描述:The path.relative() method returns the relative path from from to to (从from路径,到to路径的相对路径。)

边界:

如果from、to指向同个路径,那么,返回空字符串。
如果from、to中任一者为空,那么,返回当前工作路径。
例子如下:

const path = require('path');
 
var p1 = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
console.log(p1);  
// 输出 "../../impl/bbb"
 
var p2 = path.relative('/data/demo', '/data/demo');
console.log(p2);  
// 输出 ""
 
var p3 = path.relative('/data/demo', '');
console.log(p3);  
// 输出 ..\..\Study\Nodejs\mycode\path
平台相关接口/属性
以下属性、接口,都跟平台的具体实现相关。也就是说,同样的属性、接口,在不同平台上的表现不同。

path.posix:path相关属性、接口的linux实现。
path.win32:path相关属性、接口的win32实现。
path.sep:路径分隔符。在linux上是/,在windows上是\。
path.delimiter:path设置的分割符。linux上是:,windows上是;。
注意,当使用 path.win32 相关接口时,参数同样可以使用/做分隔符,但接口返回值的分割符只会是\。

例子如下:

> path.win32.join('/tmp', 'fuck')
'\\tmp\\fuck'
> path.win32.sep
'\\'
> path.win32.join('\tmp', 'demo')
'\\tmp\\demo'
> path.win32.join('/tmp', 'demo')
'\\tmp\\demo'
path.delimiter 
linux系统例子:

console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter)
// returns ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
windows系统例子:

console.log(process.env.PATH)
// 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
 
process.env.PATH.split(path.delimiter)
// returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
const这个关键字是ES6里面定义的常量,不可改变。

1 const url = require("url");
  url一共提供了三个方法,分别是url.parse();  url.format();  url.resolve();

  1 url.parse(urlString,boolean,boolean)

  parse这个方法可以将一个url的字符串解析并返回一个url的对象

  参数:urlString指传入一个url地址的字符串

     第二个参数(可省)传入一个布尔值,默认为false,为true时,返回的url对象中,query的属性为一个对象。

     第三个参数(可省)传入一个布尔值,默认为false,为true时,额,我也不知道有什么不同,可以去看看API。

 

 

例子1,url.parse只传一个参数的情况。

 1 url.parse("http://user:[email protected]:8080/p/a/t/h?query=string#hash");
 2 /*
 3 返回值:
 4 {
 5   protocol: 'http:',
 6   slashes: true,
 7   auth: 'user:pass',
 8   host: 'host.com:8080',
 9   port: '8080',
10   hostname: 'host.com',
11   hash: '#hash',
12   search: '?query=string',
13   query: 'query=string',
14   pathname: '/p/a/t/h',
15   path: '/p/a/t/h?query=string',
16   href: 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'
17  }
18 没有设置第二个参数为true时,query属性为一个字符串类型
19 */


例子2,url.parse第二个参数为true的情况

 

 1 url.parse("http://user:[email protected]:8080/p/a/t/h?query=string#hash",true);
 2 /*
 3 返回值:
 4  {
 5   protocol: 'http:',
 6   slashes: true,
 7   auth: 'user:pass',
 8   host: 'host.com:8080',
 9   port: '8080',
10   hostname: 'host.com',
11   hash: '#hash',
12   search: '?query=string',
13   query: { query: 'string' },
14   pathname: '/p/a/t/h',
15   path: '/p/a/t/h?query=string',
16   href: 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'
17  }
18 返回的url对象中,query属性为一个对象
19 */
 

  2 url.format(urlObj)

  format这个方法是将传入的url对象编程一个url字符串并返回

  参数:urlObj指一个url对象

 

例子3,url.format

url.format({
    protocol:"http:",
    host:"182.163.0:60",
    port:"60"
});
/*
返回值:
'http://182.163.0:60'
*/
  3 url.resolve(from,to)

  resolve这个方法返回一个格式为"from/to"的字符串,在宝宝看来是对传入的两个参数用"/"符号进行拼接,并返回

console.info("------   url parse()   ------");
console.info();
var url = require('url');// TODO: 引入路径处理模块
/**
 * 测试 url.resolve() 函数方法
 * @type {string}
 */
console.info(url.resolve('/one/two/three', 'four'));
console.info(url.resolve('/one/two/three/', 'four'));
console.info(url.resolve('/one/two/three', '/four'));
console.info(url.resolve('/one/two/three/', '/four'));
console.info(url.resolve('http://www.example.com/one', 'two'));
console.info(url.resolve('http://www.example.com/one', '/two'));
console.info(url.resolve('http://www.example.com/one/', 'two'));
console.info(url.resolve('http://www.example.com/one/', '/two'));
/one/two/four
/one/two/three/four
/four
/four
http://www.example.com/two
http://www.example.com/two
http://www.example.com/one/two
http://www.example.com/two
1 querystring.parse(str,separator,eq,options)

parse这个方法是将一个字符串反序列化为一个对象。

参数:str指需要反序列化的字符串;

   separator(可省)指用于分割str这个字符串的字符或字符串,默认值为"&";

   eq(可省)指用于划分键和值的字符或字符串,默认值为"=";

   options(可省)该参数是一个对象,里面可设置maxKeys和decodeURIComponent这两个属性:

      maxKeys:传入一个number类型,指定解析键值对的最大值,默认值为1000,如果设置为0时,则取消解析的数量限制;

      decodeURIComponent:传入一个function,用于对含有%的字符串进行解码,默认值为querystring.unescape。在官方API的例子中,使用gbkDecodeURIComponent这个方法会报错,显示gbkDecodeURIComponent is no defined,这是因为在使用这个gbkDecodeURIComponent这个方法之前需要先进行定义。在API中也写了Assuming gbkDecodeURIComponent function already exists...这句话的意思是”假设这个gbkDecodeURIComponent方法已经存在”。

例子1,querystring.parse

 1 querystring.parse("name=whitemu&sex=man&sex=women");
 2 /*
 3 return:
 4 { name: 'whitemu', sex: [ 'man', 'women' ] }
 5 */
 6 querystring.parse("name=whitemu#sex=man#sex=women","#",null,{maxKeys:2});
 7 /*
 8 return:
 9 { name: 'whitemu', sex: 'man' }
10 */


2 querystring.stringify(obj,separator,eq,options)

stringify这个方法是将一个对象序列化成一个字符串,与querystring.parse相对。

参数:obj指需要序列化的对象

   separator(可省)用于连接键值对的字符或字符串,默认值为"&";

   eq(可省)用于连接键和值的字符或字符串,默认值为"=";

   options(可省)传入一个对象,该对象可设置encodeURIComponent这个属性:

      encodeURIComponent:值的类型为function,可以将一个不安全的url字符串转换成百分比的形式,默认值为querystring.escape()。

例子2,querystring.stringify

querystring.stringify({name: 'whitemu', sex: [ 'man', 'women' ] });
/*
return:
'name=whitemu&sex=man&sex=women'
*/
querystring.stringify({name: 'whitemu', sex: [ 'man', 'women' ] },"*","$");
/*
return:
'name$whitemu*sex$man*sex$women'
*/


3 querystring.escape(str)

escape可使传入的字符串进行编码

例子3,querystring.escape

querystring.escape("name=慕白");
/*
return:
'name%3D%E6%85%95%E7%99%BD'
*/
4 querystring.unescape(str)

unescape方法可将含有%的字符串进行解码

例子4,querystring.unescape

querystring.unescape('name%3D%E6%85%95%E7%99%BD');
/*
return:
'name=慕白'
*/
总结:

  querystring这个模块相对的还是比较简单,仅有4个方法。

  querystring.stringify序列化;

  querystring.parse反序列化;

  querystring.escape编码;

  querystring.unescape解码;
————————————————
版权声明:本文为CSDN博主「liudongdong19」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liudongdong19/article/details/81353159

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章