nodejs系列(5)网址处理模块url和路径处理模块path相关应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zeping891103/article/details/79179891

网址处理模块url

Node.js提供了处理网址的API,require('url')。该API主要作用为处理网址相关的数据。下面例子展示了如何获取当前网址:

/********************url测试用例********************/
app.get('/url_get', function(req, res) {
	var url = require('url');
	var host = "http://" + req.headers.host; // 'http://localhost:8081'
	var path = url.parse(req.url).path; // '/url_get?abc=123'
	var pathname = url.parse(req.url).pathname; // '/url_get'
	res.end(host + path); // http://localhost:8081/url_get?abc=123
});
浏览器访问图如下:


有关url的其它接口功能,统一整理如下:

var url = require('url');
var href = "http://localhost:8081/url_get?abc=123";
var myURL = new url.URL(href);
console.log(myURL.href); // http://localhost:8081/url_get?abc=123
console.log(myURL.origin); // http://localhost:8081
console.log(myURL.host); // localhost:8081
console.log(myURL.hostname); // localhost
console.log(myURL.port); // 8081
console.log(myURL.pathname); // /url_get
console.log(myURL.protocol); // http:
console.log(myURL.search); // ?abc=123
console.log(myURL.searchParams.get('abc')); // 123

(注)上例中通过.searchParams.get( )方法来获取传参,实际在Express框架中无需如此复杂,Get方法传递参数的使用req.query.abc获取,Post方法传递参数的使用req.body.abc获取即可。


路径处理模块path

Node.js也提供了用于处理路径相关的API,用require('path')表示。最常用的如获取文件目录路径、获取文件后缀名、获取文件名、格式化文件路径等方法。

path.basename(path[, ext])

返回一个 path 的最后一部分。

console.log(path.basename('/foo/bar/baz/asdf/quux.html')); //quux.html
console.log(path.basename('/foo/bar/baz/asdf/quux.html', '.html')); //去后缀,quux


path.sep 

目录分隔符

console.log('foo\\bar\\baz'.split(path.sep)); // [ 'foo', 'bar', 'baz' ]


扫描二维码关注公众号,回复: 3747481 查看本文章
path.dirname(path) 

返回目录路径

console.log(path.dirname('/foo/bar/baz/asdf/quux.html')); // '/foo/bar/baz/asdf'

path.extname(path)

返回后缀名

console.log(path.extname('index.html')); // .html
console.log(path.extname('index.png')); // .png

path.parse(path)

返回一个对象,对象的属性表示 path 的元素。

console.log(path.parse('C:\\path\\dir\\file.txt'));
// 返回:
// { root: 'C:\\',
//   dir: 'C:\\path\\dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' }

path.relative(from, to)

返回从 from 到 to 的相对路径(基于当前工作目录)

console.log(path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')); // ..\..\impl\bbb

path.normalize(path)

当发现多个连续的路径分隔符时(如 POSIX 上的 / 与 Windows 上的 \ 或 /),它们会被单个的路径分隔符(POSIX 上是 /,Windows 上是 \)替换。 末尾的多个分隔符会被保留。

console.log(path.normalize('C:\\temp\\\\foo\\bar')); // C:\temp\foo\bar
console.log(path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar')); //当有两种分隔符时,用path.win32,C:\temp\foo\bar

小贴士

1.global全局变量

global.hello = "hello"; //global全局变量,可以在任何js中引用
console.log(global.hello);

2.获取当前js文件绝对路径

console.log(__filename);

3.获取当前js文件的所在目录

console.log(__dirname);

4.获取node.exe绝对路径

console.log(process.execPath); // C:\Program Files\nodejs\node.exe

5.获取项目目录绝对路径

console.log(process.cwd()); //C:\Users\Administrator\Documents\HBuilderProject\NodeExpress

猜你喜欢

转载自blog.csdn.net/zeping891103/article/details/79179891