node.js - 可读流 Node : the _read method with the Number of bytes param

标签 node.js stream

我正在尝试找出 Node 中的流并尝试 the stream handbook 中的一些示例

我正在尝试 _read可读流的方法。它说,它接受一个参数,该参数是消费者想要读取的字节数。

我有两个问题。

  • 是消费者想要读取“水印”的字节数
  • 为什么当我将 _read 与参数一起使用时会出现错误。

这是我的代码。

var Readable = require('stream').Readable;
var rs = Readable();

var c = 97;
rs._read = function (5) {
    rs.push(String.fromCharCode(c++));
    if (c > 'z'.charCodeAt(0)) {
        rs.push('\n');
        rs.push(null);
    }
};

setTimeout(function () {
  rs.pipe(process.stdout);
}, 2000);

这就是错误

/Users/nikhilkuria/Dev/git/node_demo/streams/streamRead.js:5
rs._read = function (5) {
                 ^
SyntaxError: Unexpected number
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

最佳答案

function (5) { 不是创建函数的有效语法。您需要传递变量名称而不是数字,如下所示:function (a) {

您看到的错误是由于 JavaScript 不允许变量名称以数字开头或仅由数字组成。

关于node.js - 可读流 Node : the _read method with the Number of bytes param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885950/

相关文章:

c - 在文本文件上写入 Twitter 流时出现段错误

java - 为什么这段代码等待 1000 毫秒而不是 500 毫秒?

api - 使用 websocket 管理 Binance 订单簿

Android - 如何使用 Mediaplayer 类读取视频流

node.js - mongoDB/NodeJS save() 方法不起作用

node.js - 如何使用动态键附加到字典?

javascript - node.js 将 imagemagick 的输出上传到 AWS S3

node.js - 无法让 Mongoose.js 子文档数组填充

javascript - 从今天起获得先前值(value)的最佳方式

node.js - 在 Node.js 中发出 HTTP 请求并接收 multipart/x-mixed-replace 响应