一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

十六、知识点补充

22、gzip
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')

/*
* Accept-Encoding: gzip:浏览器告诉服务器所支持的编码
* Content-Encoding: gzip:服务器告诉浏览器所提供的编码
*/

const gzip = (src) => {
  fs.createReadStream(src)
    .pipe(zlib.createGzip())
    .pipe(fs.createWriteStream(src + '.gz'))
}
// gzip(path.join(__dirname, 'abc.txt'))

const gunzip = (src) => {
  fs.createReadStream(src)
    .pipe(zlib.createGunzip())
    .pipe(fs.createWriteStream(path.join(__dirname, path.basename(src, '.gz'))))
}
gunzip(path.join(__dirname, 'abc.txt.gz'))
23、crypto
/**
 * 1、可以用来校验要下载的文件是否被改动过
 * 2、对密码进行加密
 */
const crypto = require('crypto')
// console.log(crypto.getHashes())
// const hash = crypto.createHash('sha1')
// openssl genrsa -out rsa_private.key 1024:命令生成密钥
const hash = crypto.createHmac('sha1', 'linlong')
hash.update('hello')
hash.update('world')
// sha1:40位
// md5:32位
// hex:十六进制
console.log(hash.digest('hex'))
const crypto = require('crypto')
const data = 'hello world'
const password = 'linlong'

const cipher = crypto.createCipher('aes-256-cbc', password);
cipher.update(data, 'utf8')
const result = cipher.final('hex');
console.log(result)

const decipher = crypto.createDecipher('aes-256-cbc', password);
decipher.update(result, 'hex')
const r = decipher.final('utf8');
console.log(r)
24、uncaughtException
// 专门用来处理未捕获的异常(容易引起内存泄露,不常用)
process.on('uncaughtException', (error) => {
  console.log(error)
})
setTimeout(() => {
  console.log('程序')
}, 3000)
throw new Error('异常')
25、detached
const childProcess = require("child_process")
const fs = require('fs')
const path = require('path')

const fd = fs.openSync(path.join(__dirname, 'msg.txt'), 'w', 0o666)
const p1 = childProcess.spawn('node', ['test.js'], {
  stdio: ['ignore', fd, 'ignore'],
  cwd: path.join(__dirname),
  detached: true
})
// 默认情况下父进程会等待所有的子进程退出后才可以退出
// detached配置、unref方法,可以让父进程不用等待而直接退出
p1.unref()
let i = 0
const timer = setInterval(() => {
  process.stdout.write(new Date().toUTCString() + '\r\n')
  if (i++ >= 10) {
    clearInterval(timer)
  }
}, 1000)
26、fork
// fork exec execFile 它们其实都是基于spawn的改进方法
const {fork, spawn} = require('child_process')
/*
function fork(modulepath, args, options) {
  const {silent} = options
  const opts = Object.assign({}, options)
  if (silent) {
    opts.stdio = ['ignore', 'ignore', 'ignore']
  } else {
    opts.stdio = [process.stdin, process.stdout, process.stderr]
  }
  spawn('node', [modulepath, ...args], opts)
}
*/
const child = fork('test.js', ['zfpx'], {
  cwd: __dirname,
  silent: false
})
child.on('message', function (data) {
  console.log(data)
})
child.send({name: 'zfpx'})
process.stdout.write('world' + process.argv[2])
process.on('message', function (msg) {
  process.send('子进程:' + JSON.stringify(msg))
})
27、exec
const {exec} = require('child_process')
const path = require('path')
const p1 = exec('node test.js a b c', {
  cwd: path.resolve(__dirname),
  encoding: 'utf8',
  // timeout: 10,
  maxBuffer: 1024 * 1024
}, function (error, stdout, stderr) {
  console.log(error)
  console.log(stdout)
})
for (let i = 2; i < process.argv.length; i++) {
  process.stdout.write(process.argv[i] + '\r\n')
}
28、execFile
const {execFile} = require('child_process')
const path = require('path')
const p1 = execFile('node', ['test.js', 'a', 'b', 'c'], {
  cwd: path.resolve(__dirname),
  encoding: 'utf8',
  // timeout: 10,
  maxBuffer: 1024 * 1024
}, function (error, stdout, stderr) {
  console.log(error)
  console.log(stdout)
})
for (let i = 2; i < process.argv.length; i++) {
  process.stdout.write(process.argv[i] + '\r\n')
}
posted on 2024-05-21 17:39  一路繁花似锦绣前程  阅读(6)  评论(0编辑  收藏  举报