简体   繁体   English

使用节点可写流写入数据

[英]Using node Writable stream write data

this is the code: 这是代码:

var stream = require('stream')
var ws = new stream.writable()
ws.write('hello')

but when run above code, it prompt 但是当运行上面的代码时,它会提示

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: not implemented
    at Writable._write (_stream_writable.js:435:6)
    at doWrite (_stream_writable.js:307:12)
    at writeOrBuffer (_stream_writable.js:293:5)
    at Writable.write (_stream_writable.js:220:11)
    at Object.<anonymous> (/Users/suoyong/Desktop/test.js:15:4)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)

what's wrong with this code, thanks advance! 此代码有什么问题,谢谢您!

You need to implement your own writable stream. 您需要实现自己的可写流。 Take a look at this post for how to create your own: How to implement a writable stream 看看这篇文章,了解如何创建自己的文章: 如何实现可写流

From the article - you can try something like this: 从这篇文章中-您可以尝试这样的事情:

var stream = require('stream');

var ws = new stream.Writable({
  write: function(chunk, encoding, next) {
    console.log(chunk.toString());
    next();
  }
});
ws.write('hello');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM