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

导航

 

十六、知识点补充

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')
}
29、多语言切换
* 请求头:Accept-Language: zh-CN,zh;q=0.9
    - 浏览器告诉服务器所支持的语言,以,分割,q表示权重,没有则默认q=1
* 响应头:Content-Language: zh-CN
30、图片防盗链
* 请求头:Referer(Refer): https://www.baidu.com/
    - html标签中的请求地址默认会带上此请求头,值为当前页面的url地址
31、代理服务器
const proxy = require('http-proxy')
const http = require('http')
const url = require('url')
const proxyServer = proxy.createProxyServer()
// 正向代理:帮助或代理局域网内的用户访问外网
// 反向代理:用来代理局域网内的服务器的
const server = http.createServer(function (req, res) {
  /*
  function web(req, res, options) {
    let {host, port, pathname} = url.parse(req.url)
    let opts = {
      host,
      port,
      method: req.method,
      path: pathname,
      header: req.headers
    }
    opts.host = options.target
    http.request(opts, function (response) {
      response.pipe(res)
    })
  }
  */
  proxyServer.web(req, res, {
    target: 'http://localhost:9000'
  })
}).listen(8000)
const http = require('http')

const server = http.createServer(function (req, res) {
  res.end('9000')
}).listen(9000)
32、User-Agent
const http = require('http')
const parse = require('user-agent-parser');
const server = http.createServer(function (req, res) {
  const userAgent = req.headers['user-agent']
  console.log(userAgent)
  /**
   * User Agent中文名为用户代理,简称UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作
   * 系统及版本、CPU类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
   */
  const userAgentObj = parse(userAgent);
  res.end(JSON.stringify(userAgentObj))
})
server.listen(8080)
33、cookie
* 签名算法是为了防篡改
* document.cookie增删改是一个键值(【;】分隔的第一个是键值),查是所有键值(以【; 】分隔)
posted on 2024-05-21 17:39  一路繁花似锦绣前程  阅读(32)  评论(0)    收藏  举报