3.nodejs(三) 常用API (2)

URLSearchParams
new URLSearchParams();

var modUrl = require("url"); var params = new modUrl.URLSearchParams(); console.log(params); params.append("user","aaa"); params.append("pass","123"); //append添加数据 // user=aaa&pass=123 console.log(params); console.log(params.toString());

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node url4 URLSearchParams {} URLSearchParams { 'user' => 'aaa', 'pass' => '123' } user=aaa&pass=123 url5.js //node 10以上,才可以 var params = new URLSearchParams(); params.append("user","aaa"); params.append("pass","123"); // user=aaa&pass=123 console.log(params.toString());

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node url5 user=aaa&pass=123 3.path path:

path.parse(path)
path.format(pathObject)

path.basename(path)
path.dirname(path) 路径
path.extname(path)

path.join() 相对路径 路径拼接
path.relative() 获取的相对路径 路线图
path.resolve() 绝对路径 路径拼接 碰到绝对路径 会替换

__dirname 绝对路径
__filename 绝对地址包含文件名

global 全集对象 相当于 window

path1.js var path = require("path"); //win: \ linux / var str = "D:\\wamp64\\www\\20180711\\path.js"; //window要用双斜线,向右撇 var str = "D:/wamp64/www/20180711/path.js"; var pathObj = path.parse(str); console.log(pathObj);//字符串转对象 console.log(path.format(pathObj));//对象转字符串

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node path1 { root: 'D:/', dir: 'D:/wamp64/www/20180711', base: 'path.js', ext: '.js', name: 'path' } D:/wamp64/www/20180711\path.js path2.js var path = require("path"); var str = "D:/wamp64/www/20180711/path.js"; console.log(path.parse(str)); console.log("--------------------------"); console.log(path.basename(str));//文件名 console.log(path.dirname(str)); //路径 console.log(path.extname(str));//文件后缀 带点 console.log("--------------------------"); console.log(__dirname); console.log(__filename);

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node path2 { root: 'D:/', dir: 'D:/wamp64/www/20180711', base: 'path.js', ext: '.js', name: 'path' } -------------------------- path.js D:/wamp64/www/20180711 .js -------------------------- D:\workspace\7.11 D:\workspace\7.11\path2.js path31.js var path = require("path"); console.log("www"+"/index.html");//只是拼接 console.log(path.join("www","/index.html")); console.log(path.join("www","/////index.html"));//自动去除多余的/ console.log(path.join("www","index.html"));//自动添加/

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node path31 www/index.html www\index.html www\index.html www\index.html path32.js var path = require("path"); console.log("www"+"/index.html"); console.log(path.join("\\a","b","c")); console.log(path.join("/a","b","c")); console.log(path.join("a","/b","../c")); //../返回上级目录

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node path32 www/index.html \a\b\c \a\b\c a\c path33.js var path = require("path"); console.log(path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')); console.log(path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb'));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node path33 ..\..\impl\bbb ..\..\impl\bbb path34.js var path = require("path"); /* resolve返回绝对路径,参数前不加/, 在后面添加,加/直接替换全部 */ console.log(path.resolve("a","b","c")); console.log(path.resolve("a","/b","/c")); console.log(path.resolve("a/b/c","../d","../e"));

res:

my@my-THINK MINGW64 /d/workspace/7.11 $ node path34 D:\workspace\7.11\a\b\c D:\c D:\workspace\7.11\a\b\e 4.Buffer

Buffer 二进制流
操作方式和字符串类似

16进制 0-F

"a".chatCodeAt(0).toString(16); ascii:97 hex:61

97: a (ASCII)

String.fromCharCode(code);//ASCII转字符

中文的范围 4e00 - 9fa5
\u4e00(直接转成中文)
0X4e00(转成十进制)

hex: 16进制

Buffer.from(...); new Buffer(废弃)

buf1.js var str = "abc"; //var buf = new Buffer(str); var buf = Buffer.from(str); console.log(buf); //16进制 中文的范围 4e00 - 9fa5 console.log("a".charCodeAt(0).toString(16));

res:

$ node buf1 <Buffer 61 62 63> 61 buf2.js const buf = Buffer.from('abc', 'ascii'); console.log(buf); console.log(buf.toString('hex')); console.log(buf.toString('base64'));

res:

$ node buf2 <Buffer 61 62 63> 616263 YWJj buf3.js const buf = Buffer.from([1, 2, 3]); console.log(buf); for(var i = 0; i < buf.length; i++){ console.log(buf[i]); } // Prints: // 1 // 2 // 3 for (const b of buf) { console.log(b); }

res:

$ node buf3 <Buffer 01 02 03> 1 2 3 1 2 3 buf4.js var buf = Buffer.from([0x61, 0x62, 0x63]); console.log(buf.toString()); var buf = Buffer.from("abc"); console.log(buf.toString());

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyyfz.html