标签:
一、处理JSON
1、将JavaScript数据转换为JSON对象(序列化)
2、将JSON数据转换为JavaScript对象(逆序列化)
二、Buffer模块缓冲数据(使用两位16进制表示一字节)
1、创建缓冲区
Buffer.from(array)
: returns a new Buffer
containing a copy of the provided octets.Buffer.from(arrayBuffer[, byteOffset [, length]])
:returns a new Buffer
that shares the same allocated memory as the given ArrayBuffer
.Buffer.from(buffer)
: returns a new Buffer
containing a copy of the contents of the given Buffer
.Buffer.from(str[, encoding])
: returns a new Buffer
containing a copy of the provided string.Buffer.alloc(size[, fill[, encoding]])
: returns a "filled" Buffer
instance of the specified size. This method can be significantly slower than Buffer.allo
cU
nsafe(size)
but ensures that newly created Buffer
instances never contain old and potentially sensitive data.Buffer.allocUnsafe(size)
and Buffer.allocUnsafeSlow(size):
each return a new Buffer
of the specified size
whose content must be initialized using either buf.fill(0)
or written to completely.附:当前Node.js(6.2.2)支持的编码(encoding)方式:
‘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、写入缓冲区
3、读取缓冲区
4、确定缓冲区长度
5、复制缓冲区
6、对缓冲区切片
7、拼接缓冲区
8、比较缓冲区
9、判断是否为缓冲区
10、判断是否为支持的编码方式
三、Stream模块来传送数据
1、Readable流
常见实例:
对象事件:
对象方法:
2、Writeable流
常见实例:同上。
对象事件:
对象方法:
3、Duplex流
4、Transform流
标签:
原文地址:http://www.cnblogs.com/realsoul/p/5616510.html