1. global.process
常量
const {argv, argv0, execArgv, execPath} = process
// argv 数组
argv.forEach(item => {
console.log(item)
})
// 打印如下
/**
* /usr/local/bin/node node启动的路径
* /Users/nardo/Documents/study/node-study/process.js 执行node命令的脚本路径
*/
// argv还可以有扩展的参数, 执行 node process.js --test a b=2
// 注意写在文件名之前的参数不会被打印
/**
* /usr/local/bin/node node启动的路径
* /Users/nardo/Documents/study/node-study/process.js 执行node命令的脚本路径
* --test
* a
* b=2
*/
// argv0 保存了argv第一个值的引用
console.log('argv0', argv0)
// 执行node --inspect process.js --test a b=2
console.log('execArgv', execArgv) // [ '--inspect' ]
// execPath
console.log('execPath', execPath) // /usr/local/bin/node node启动的路径
// process.env
const { env } = process
// process.cwd 打印出当前process的路径
console.log('cwd', process.cwd())
// /Users/nardo/Documents/study/node-study
2. path
模块
// 1. normalize 格式化路径
const {
normalize
} = require('path')
console.log(normalize('/usr//local/bin')) // /usr/local/bin
console.log(normalize('/usr/../local/bin')) // /local/bin
// 2. join 拼接路径,调用时默认会调用normalize
const {
join
} = require('path')
console.log(join('/usr', '../local', '/bin')) // /usr/bin
// 3. resolve 将相对路径转为绝对路径
const {
resolve
} = require('path')
console.log(resolve('./')) // /Users/nardo/Documents/study/node-study
// 4. basename : 文件名, dirname: 路径不含文件名, extname: 拓展名
const {
basename,
dirname,
extname
} = require('path')
const filePath = '/usr/bin/node/no.js'
console.log(basename(filePath)) // no.js
console.log(dirname(filePath)) // /usr/bin/node
console.log(extname(filePath)) // .js
// 5. parse: 将路径解析成上述3个参数,format: 相反
const {
parse,
format
} = require('path')
let ret = parse(filePath)
console.log(ret)
/**
* { root: '/',
dir: '/usr/bin/node',
base: 'no.js',
ext: '.js',
name: 'no' }
*/
// 注意: pathObject的优先级
// pathObject.dir存在时,忽略pathObject.root
// pathObject.base存在时,pathObject.ext和pathObject.name会被忽略
console.log(format({
root: '/ignored',
dir: '/bin/node/dir',
base: 'yes.js'
}))
// /bin/node/dir/yes.js root被忽略了
console.log(format({
dir: '/bin/node/image',
base: 'xmly.png',
name: 'not_xmly',
ext: 'jpg'
}))
// /bin/node/image/xmly.png ext和name被忽略了
/* 6. sep: 写path时的分隔符
* delimiter: 指PATH下的路径分隔符,macOS为 ':' windows为 ';'
* win32:
* posix:
*/
const {
sep,
delimiter,
win32,
posix
} = require('path')
console.log('sep:', sep) // /
console.log('win.sep', win32.sep) // \
console.log('PATH', process.env.PATH) // PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
console.log('delimiter', delimiter) // :
console.log('win.delimiter', win32.delimiter) // ;
3. paths
// function (exports, require, module, __filename, __dirname) {}
// 1. __dirname __filename
// const mod = require('./module_exports')
const path = require('path')
console.log('__dirname: ', __dirname) // /Users/nardo/Documents/study/node-study
console.log('__filename: ', __filename) // /Users/nardo/Documents/study/node-study/5_paths.js
console.log('process.cwd(): ', process.cwd()) // /Users/nardo/Documents/study/node-study
console.log('./ ', path.resolve('./')) // /Users/nardo/Documents/study/node-study
// 但如果此时不在当前目录执行脚本,而是返回上一层级
// $ cd ..
// 打印结果是:
// __dirname: /Users/nardo/Documents/study/node-study
// __filename: /Users/nardo/Documents/study/node-study/5_paths.js
// process.cwd(): /Users/nardo/Documents/study
// ./ /Users/nardo/Documents/study
// 可以看到 process.cwd()和./变成了执行node命令的路径
/** 总结:
* 1. __dirname, __filename总是返回文件的绝对路径
* 2. process.cwd() 总是返回node执行所在文件目录
* 3. './' 在require方法中总是相对当前文件所在文件夹,但在其他地方和process.cwd()一样,相对node启动文件夹目录
*/
4. global.Buffer
// Buffer 全局变量
// 1. 用来处理二进制数据流
// 2. 实例类似整数数组,大小固定
// 3. C++代码在V8堆外分配物理内存
console.log(Buffer.alloc(10)) // <Buffer 00 00 00 00 00 00 00 00 00 00>
console.log(Buffer.alloc(5, 1)) // <Buffer 01 01 01 01 01>
console.log(Buffer.allocUnsafe(5, 1)) // <Buffer 70 83 81 03 01>
console.log(Buffer.from([1, 2, 3])) // <Buffer 01 02 03>
console.log(Buffer.from('test')) // <Buffer 74 65 73 74>
console.log(Buffer.from('test', 'base64')) // <Buffer b5 eb 2d>
// Buffer.byteLength
console.log(Buffer.byteLength('test')) // 4
console.log(Buffer.byteLength('测试')) // 6
// Buffer.isBuffer
console.log(Buffer.isBuffer({})) // false
console.log(Buffer.isBuffer(Buffer.from([1,2]))) // true
// Buffer.concat
const buf1 = Buffer.from('This ')
const buf2 = Buffer.from('is ')
const buf3 = Buffer.from('a ')
const buf4 = Buffer.from('test ')
const buf5 = Buffer.from('! ')
const lastBuf = Buffer.concat([buf1, buf2, buf3, buf4, buf5])
console.log(lastBuf) // <Buffer 54 68 69 73 20 69 73 20 61 20 20 74 65 73 74 20 21 20>
// buf.length
var buff = Buffer.from('This is a buffer')
console.log(buff.length) // 16 from方法调用时直接由内容决定buffer所占bytes
var buf = Buffer.alloc(10)
buf[0] = 2 // 该buffer占10bytes,但是只填充了1个
console.log(buf, buf.length) // <Buffer 02 00 00 00 00 00 00 00 00 00> , 10
// buf.toString()
console.log(buff.toString()) // This is a buffer
console.log(buff.toString('base64')) // VGhpcyBpcyBhIGJ1ZmZlcg==