处理数据(I/O)

一、处理JSON

  1、将JavaScript数据转换为JSON对象(序列化)

  • JSON.stringify(Object)

  2、将JSON数据转换为JavaScript对象(逆序列化)

  • JSON.parse(stringJSON)

二、Buffer模块缓冲数据(使用两位16进制表示一字节)

  1、创建缓冲区

  附:当前Node.js(6.2.2)支持的编码(encoding)方式:

  • 'ascii' - for 7-bit ASCII data only. This encoding method is very fast and will strip the high bit if set.
  • 'utf8' - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.

  • 'utf16le' - 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.

  • 'ucs2' - Alias of 'utf16le'.

  • 'base64' - Base64 string encoding. When creating a buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC 4648, Section 5.

  • 'binary' - A way of encoding the buffer into a one-byte (latin-1) encoded string. The string 'latin-1' is not supported. Instead, pass 'binary' to use 'latin-1' encoding.

  • 'hex' - Encode each byte as two hexadecimal characters.

  2、写入缓冲区

  • buf.fill(value[, offset[, end]][, encoding])
  • buf.write(string[, offset[, length]][, encoding])

  3、读取缓冲区

  • buf.entries()  //Return: <Iterator>
  • buf.indexOf(value[, byteOffset][, encoding])
  • buf.includes(value[, byteOffset][, encoding])
  • buf.keys()
  • buf.values()
  • buf.lastIndexOf(value[, byteOffset][, encoding])
  • buf.toString([encoding[, start[, end]]])
  • buf.toJSON()
  • for...of

  4、确定缓冲区长度

  • Buffer.byteLength(string[, encoding])
  • buf.length

  5、复制缓冲区

  • sourceBuf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])

  6、对缓冲区切片

  • buf.slice([start[, end]])

  7、拼接缓冲区

  • Buffer.concat(list[, totalLength])

  8、比较缓冲区

  • Buffer.compare(buf1, buf2)
  • buf.equals(otherBuffer)

  9、判断是否为缓冲区

  • Buffer.isBuffer(obj)

 10、判断是否为支持的编码方式

  • Buffer.isEncoding(encoding)

三、Stream模块来传送数据

  1、Readable流

  常见实例:

  • 在客户端的HTTP响应
  • 在服务器的HTTP请求
  • fs读取流
  • zlib流
  • crypto(加密)流
  • TCP套接字
  • 子进程的stdout和stderr
  • process.stdin

  对象事件:

  • readable:在数据块可以从流中读取的时候发出
  • data:类似于readable;不同之处在于,当数据的事件处理程序被连接时,流被转变成流动的模式,并且数据处理程序被连续的调用,直到所有数据都被用尽
  • end:当数据将不再被提供时由流发出
  • close:当底层在资源,如文件,已关闭时发出
  • error:当在接收数据中出现错误时发出

  对象方法:

  • read([size]):从流中读取数据。如果指定size参数,那么读取的数据将仅限于那个字节数
  • setEncoding(encoding):设置read()请求读取返回的String时使用的编码
  • pause():暂停从该对象发出的data事件
  • resume():恢复从该对象发出的data事件
  • pipe(destination[,options]):把这个流的输出传输到一个由destination(目的地)指定的Writeable流对象。options是一个JavaScript对象。
  • unpipe([destination]):从Writeable目的地断开这一对象

  2、Writeable流

  常见实例:同上。

  对象事件:

  • drain:在write()调用后返回false,当准备好开始写更多的数据时,发出此事件通知监听器
  • finish:当end()在Writeable对象上被调用,所有数据都被刷新,并且不会有更多的疏忽被接受时发出此事件
  • pipe:当pipe()方法子在Readable流上被调用,以添加此Writeable为目的地时,发出此事件、
  • unpipe:当unpipe()方法在Readable流上被调用,以删除此Writeable为目的地时,发出此事件

  对象方法:

  • wirte(chunk[,ecoding][,callback]):将数据写入流对象的数据位置,若指定callback,那么它将在数据已被刷新后被调用
  • end([chunk][,encoding][callback]):与write()相同,除了它把Writeable对象置于不再接收数据的状态,并发送finish事件外

  3、Duplex流

  4、Transform流

All rights reserved please indicate the source if reprint---吓尿了的大肥鼠
原文地址:https://www.cnblogs.com/realsoul/p/5616510.html