searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享

Node核心模块之Stream

2023-05-23 09:45:20
29
0

Node.js V6.9.4

流(stream)在node中是一个用于处理流数据的抽象接口,node中很对对象都是基于流的,如HTTP服务器,process.stdout请求
流是可读、可写的,或者既可读有可写,并且所有流都是EventEmitter的实例
流主要用于实现一个copy方法.

const stream = require('stream')

1.流的四种类型

  •  Readable: 可以读取数据的流
  • Writable: 可以写数据的流
  •  Duplex: 既可以读数据的流,有可以写数据的流
  • Transform: 可以在写入和读取数据时修改或转换数据的双工流

2.buffer
       可读流和可写流都将数据存储在内部缓冲区buffer中,当调用stream.push(chunk)时,数据被缓冲在可读流中,如果用户没有调用stream.read()读取流,那么在缓冲区中的流将会被一直存储。
       如果读缓冲区区中的buffer达到指定大小,则再次读取的流将不会被缓存,stream.write()也一样,一旦达到阈值,将返回false。stream.pipe()方法很好的解决上面的阈值问题, pipe()的目的在于将数据的缓冲限制在可接受的水平,被称为管道。

3.一个Node.js官网例子

const http = require('http');

const server = http.createServer( (req, res) => {
  // req is an http.IncomingMessage, which is a Readable Stream
  // res is an http.ServerResponse, which is a Writable Stream

  let body = '';
  // Get the data as utf8 strings.
  // If an encoding is not set, Buffer objects will be received.
  req.setEncoding('utf8');

  // Readable streams emit 'data' events once a listener is added
  req.on('data', (chunk) => {
    body += chunk;
  });

  // the end event indicates that the entire body has been received
  req.on('end', () => {
    try {
      const data = JSON.parse(body);
      // write back something interesting to the user:
      res.write(typeof data);
      res.end();
    } catch (er) {
      // uh oh!  bad json!
      res.statusCode = 400;
      return res.end(`error: ${er.message}`);
    }
  });
});

server.listen(1337);

// $ curl localhost:1337 -d '{}'
// object
// $ curl localhost:1337 -d '"foo"'
// string
// $ curl localhost:1337 -d 'not json'
// error: Unexpected token o

上例中: 可写流(res)暴露方法(write()、end()),这两个方法被用于将数据写入流中,可读流使用EventEmitter来通知应用程序何时可以读取流中的数据 

0条评论
作者已关闭评论
c****n
2文章数
0粉丝数
c****n
2 文章 | 0 粉丝
c****n
2文章数
0粉丝数
c****n
2 文章 | 0 粉丝

Node核心模块之Stream

2023-05-23 09:45:20
29
0

Node.js V6.9.4

流(stream)在node中是一个用于处理流数据的抽象接口,node中很对对象都是基于流的,如HTTP服务器,process.stdout请求
流是可读、可写的,或者既可读有可写,并且所有流都是EventEmitter的实例
流主要用于实现一个copy方法.

const stream = require('stream')

1.流的四种类型

  •  Readable: 可以读取数据的流
  • Writable: 可以写数据的流
  •  Duplex: 既可以读数据的流,有可以写数据的流
  • Transform: 可以在写入和读取数据时修改或转换数据的双工流

2.buffer
       可读流和可写流都将数据存储在内部缓冲区buffer中,当调用stream.push(chunk)时,数据被缓冲在可读流中,如果用户没有调用stream.read()读取流,那么在缓冲区中的流将会被一直存储。
       如果读缓冲区区中的buffer达到指定大小,则再次读取的流将不会被缓存,stream.write()也一样,一旦达到阈值,将返回false。stream.pipe()方法很好的解决上面的阈值问题, pipe()的目的在于将数据的缓冲限制在可接受的水平,被称为管道。

3.一个Node.js官网例子

const http = require('http');

const server = http.createServer( (req, res) => {
  // req is an http.IncomingMessage, which is a Readable Stream
  // res is an http.ServerResponse, which is a Writable Stream

  let body = '';
  // Get the data as utf8 strings.
  // If an encoding is not set, Buffer objects will be received.
  req.setEncoding('utf8');

  // Readable streams emit 'data' events once a listener is added
  req.on('data', (chunk) => {
    body += chunk;
  });

  // the end event indicates that the entire body has been received
  req.on('end', () => {
    try {
      const data = JSON.parse(body);
      // write back something interesting to the user:
      res.write(typeof data);
      res.end();
    } catch (er) {
      // uh oh!  bad json!
      res.statusCode = 400;
      return res.end(`error: ${er.message}`);
    }
  });
});

server.listen(1337);

// $ curl localhost:1337 -d '{}'
// object
// $ curl localhost:1337 -d '"foo"'
// string
// $ curl localhost:1337 -d 'not json'
// error: Unexpected token o

上例中: 可写流(res)暴露方法(write()、end()),这两个方法被用于将数据写入流中,可读流使用EventEmitter来通知应用程序何时可以读取流中的数据 

文章来自个人专栏
文章 | 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0