1. global.process常量

  1. const {argv, argv0, execArgv, execPath} = process
  2. // argv 数组
  3. argv.forEach(item => {
  4. console.log(item)
  5. })
  6. // 打印如下
  7. /**
  8. * /usr/local/bin/node node启动的路径
  9. * /Users/nardo/Documents/study/node-study/process.js 执行node命令的脚本路径
  10. */
  11. // argv还可以有扩展的参数, 执行 node process.js --test a b=2
  12. // 注意写在文件名之前的参数不会被打印
  13. /**
  14. * /usr/local/bin/node node启动的路径
  15. * /Users/nardo/Documents/study/node-study/process.js 执行node命令的脚本路径
  16. * --test
  17. * a
  18. * b=2
  19. */
  20. // argv0 保存了argv第一个值的引用
  21. console.log('argv0', argv0)
  22. // 执行node --inspect process.js --test a b=2
  23. console.log('execArgv', execArgv) // [ '--inspect' ]
  24. // execPath
  25. console.log('execPath', execPath) // /usr/local/bin/node node启动的路径
  26. // process.env
  27. const { env } = process
  28. // process.cwd 打印出当前process的路径
  29. console.log('cwd', process.cwd())
  30. // /Users/nardo/Documents/study/node-study

2. path模块

  1. // 1. normalize 格式化路径
  2. const {
  3. normalize
  4. } = require('path')
  5. console.log(normalize('/usr//local/bin')) // /usr/local/bin
  6. console.log(normalize('/usr/../local/bin')) // /local/bin
  7. // 2. join 拼接路径,调用时默认会调用normalize
  8. const {
  9. join
  10. } = require('path')
  11. console.log(join('/usr', '../local', '/bin')) // /usr/bin
  12. // 3. resolve 将相对路径转为绝对路径
  13. const {
  14. resolve
  15. } = require('path')
  16. console.log(resolve('./')) // /Users/nardo/Documents/study/node-study
  17. // 4. basename : 文件名, dirname: 路径不含文件名, extname: 拓展名
  18. const {
  19. basename,
  20. dirname,
  21. extname
  22. } = require('path')
  23. const filePath = '/usr/bin/node/no.js'
  24. console.log(basename(filePath)) // no.js
  25. console.log(dirname(filePath)) // /usr/bin/node
  26. console.log(extname(filePath)) // .js
  27. // 5. parse: 将路径解析成上述3个参数,format: 相反
  28. const {
  29. parse,
  30. format
  31. } = require('path')
  32. let ret = parse(filePath)
  33. console.log(ret)
  34. /**
  35. * { root: '/',
  36. dir: '/usr/bin/node',
  37. base: 'no.js',
  38. ext: '.js',
  39. name: 'no' }
  40. */
  41. // 注意: pathObject的优先级
  42. // pathObject.dir存在时,忽略pathObject.root
  43. // pathObject.base存在时,pathObject.ext和pathObject.name会被忽略
  44. console.log(format({
  45. root: '/ignored',
  46. dir: '/bin/node/dir',
  47. base: 'yes.js'
  48. }))
  49. // /bin/node/dir/yes.js root被忽略了
  50. console.log(format({
  51. dir: '/bin/node/image',
  52. base: 'xmly.png',
  53. name: 'not_xmly',
  54. ext: 'jpg'
  55. }))
  56. // /bin/node/image/xmly.png ext和name被忽略了
  57. /* 6. sep: 写path时的分隔符
  58. * delimiter: 指PATH下的路径分隔符,macOS为 ':' windows为 ';'
  59. * win32:
  60. * posix:
  61. */
  62. const {
  63. sep,
  64. delimiter,
  65. win32,
  66. posix
  67. } = require('path')
  68. console.log('sep:', sep) // /
  69. console.log('win.sep', win32.sep) // \
  70. console.log('PATH', process.env.PATH) // PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
  71. console.log('delimiter', delimiter) // :
  72. console.log('win.delimiter', win32.delimiter) // ;

3. paths

  1. // function (exports, require, module, __filename, __dirname) {}
  2. // 1. __dirname __filename
  3. // const mod = require('./module_exports')
  4. const path = require('path')
  5. console.log('__dirname: ', __dirname) // /Users/nardo/Documents/study/node-study
  6. console.log('__filename: ', __filename) // /Users/nardo/Documents/study/node-study/5_paths.js
  7. console.log('process.cwd(): ', process.cwd()) // /Users/nardo/Documents/study/node-study
  8. console.log('./ ', path.resolve('./')) // /Users/nardo/Documents/study/node-study
  9. // 但如果此时不在当前目录执行脚本,而是返回上一层级
  10. // $ cd ..
  11. // 打印结果是:
  12. // __dirname: /Users/nardo/Documents/study/node-study
  13. // __filename: /Users/nardo/Documents/study/node-study/5_paths.js
  14. // process.cwd(): /Users/nardo/Documents/study
  15. // ./ /Users/nardo/Documents/study
  16. // 可以看到 process.cwd()和./变成了执行node命令的路径
  17. /** 总结:
  18. * 1. __dirname, __filename总是返回文件的绝对路径
  19. * 2. process.cwd() 总是返回node执行所在文件目录
  20. * 3. './' 在require方法中总是相对当前文件所在文件夹,但在其他地方和process.cwd()一样,相对node启动文件夹目录
  21. */

4. global.Buffer

  1. // Buffer 全局变量
  2. // 1. 用来处理二进制数据流
  3. // 2. 实例类似整数数组,大小固定
  4. // 3. C++代码在V8堆外分配物理内存
  5. console.log(Buffer.alloc(10)) // <Buffer 00 00 00 00 00 00 00 00 00 00>
  6. console.log(Buffer.alloc(5, 1)) // <Buffer 01 01 01 01 01>
  7. console.log(Buffer.allocUnsafe(5, 1)) // <Buffer 70 83 81 03 01>
  8. console.log(Buffer.from([1, 2, 3])) // <Buffer 01 02 03>
  9. console.log(Buffer.from('test')) // <Buffer 74 65 73 74>
  10. console.log(Buffer.from('test', 'base64')) // <Buffer b5 eb 2d>
  11. // Buffer.byteLength
  12. console.log(Buffer.byteLength('test')) // 4
  13. console.log(Buffer.byteLength('测试')) // 6
  14. // Buffer.isBuffer
  15. console.log(Buffer.isBuffer({})) // false
  16. console.log(Buffer.isBuffer(Buffer.from([1,2]))) // true
  17. // Buffer.concat
  18. const buf1 = Buffer.from('This ')
  19. const buf2 = Buffer.from('is ')
  20. const buf3 = Buffer.from('a ')
  21. const buf4 = Buffer.from('test ')
  22. const buf5 = Buffer.from('! ')
  23. const lastBuf = Buffer.concat([buf1, buf2, buf3, buf4, buf5])
  24. console.log(lastBuf) // <Buffer 54 68 69 73 20 69 73 20 61 20 20 74 65 73 74 20 21 20>
  25. // buf.length
  26. var buff = Buffer.from('This is a buffer')
  27. console.log(buff.length) // 16 from方法调用时直接由内容决定buffer所占bytes
  28. var buf = Buffer.alloc(10)
  29. buf[0] = 2 // 该buffer占10bytes,但是只填充了1个
  30. console.log(buf, buf.length) // <Buffer 02 00 00 00 00 00 00 00 00 00> , 10
  31. // buf.toString()
  32. console.log(buff.toString()) // This is a buffer
  33. console.log(buff.toString('base64')) // VGhpcyBpcyBhIGJ1ZmZlcg==