在这里,我们将给大家分享关于node.js中stream使用教程的知识,让您更了解nodejsstream原理的本质,同时也会涉及到如何更有效地node.js–nodejs/express-strea
在这里,我们将给大家分享关于node.js中 stream使用教程的知识,让您更了解nodejs stream原理的本质,同时也会涉及到如何更有效地node.js – nodejs / express-stream stdout立即向客户端发送、node.js – NodeJs:二进制fs.createReadStream流式传输为UTF-8、node.js – util.pump(streamA,streamB)和streamA.pipe(streamB)之间有什么区别?、node.js – 如何测试对象是否是NodeJS中的Stream的内容。
本文目录一览:- node.js中 stream使用教程(nodejs stream原理)
- node.js – nodejs / express-stream stdout立即向客户端发送
- node.js – NodeJs:二进制fs.createReadStream流式传输为UTF-8
- node.js – util.pump(streamA,streamB)和streamA.pipe(streamB)之间有什么区别?
- node.js – 如何测试对象是否是NodeJS中的Stream
node.js中 stream使用教程(nodejs stream原理)
这些日子跑去学了一下OC,但是还没有学成。离转行的时间还有很长,顺便回顾一下node的知识。
每种语言来来去去的人很多,但我就离不开node。我并不是使用它开发,只是使用js相对多一些,因此还是研究node比较好,stream在node的地位是很高的。闲时也来看看这个内容,在node的路上,我还是新手。
今天下载了nodeschool的课程看,其中有一个例子。(我修改了一点点)
var concat = require(''concat-stream''); var http = require(''http''); var qs = require(''querystring''); var server = http.createServer(function (req, res) { if (req.method === ''POST'') { req.pipe(concat(function (body) { body = qs.parse(body.toString()) res.end(Object.keys(body).join(''\n'')); })); } else res.end(); }); server.listen(5000);
这题的意思是,让你把post数据反转,我就不做了,原理是一样的,只是需求不同。我们利用concat这个插件,当表单提交时,就把数据流导向concat,这个例子是取from表单post的数据。
为了测试,我使用request库。
var request = require(‘request'') request.post(‘ http://127.0.0.1:5000 ‘, {form: { “name”: “ryan”, “age” : 23 } }, function(err,res,body){ console.log(‘接收成功:'') console.log(res[‘body'']) // name age })
启动server,然后运行测试,可以看到我们把这个对象post过去了。concat-stream这个模块主要是做buffer的连接,我的理解是,当你以buffer的形式传输,这样就可以传输任何的类型了,在npmjs,它传输的是一个图片,由于我们接收完之后,就得到了这个图片的数据,我们就可以利用这个做上传,复制,道理都相通的。
- Node.js数据流Stream之Readable流和Writable流用法
- node.js中stream流中可读流和可写流的实现与使用方法实例分析
- 深入浅出了解Node.js Streams
- Node.js中你不可不精的Stream(流)
- Node.js中流(stream)的使用方法示例
- 浅谈Node.js:理解stream
- Node.js中的流(Stream)介绍
- Node.js数据流Stream之Duplex流和Transform流用法
node.js – nodejs / express-stream stdout立即向客户端发送
到目前为止我试过这个:
app.get('/path',function(req,res) { ... spw.stdout.on('data',function (data) { var str = data.toString(); res.write(str + "\n"); }); ... }
然后:
... spw.stdout.pipe(res); ...
在这两种情况下,浏览器会等待10个ping完成,然后打印结果作为一个整体.我想让他们一个接一个,怎么做呢?
(客户端只是调用… / path和console.logs的结果)
编辑:虽然我确实相信websockets是必须实现的,但我只是想知道是否有其他的方法.我看到几个混乱的SO answers和博客帖子(在this帖子,第一步OP流的日志到浏览器),这没有帮助,所以我决定去赏心悦目.
解决方法
var cp = require("child_process"),express = require("express"),app = express(); app.configure(function(){ app.use(express.static(__dirname)); }); app.get('/msg',res){ res.writeHead(200,{ "Content-Type": "text/event-stream","Cache-control": "no-cache" }); var spw = cp.spawn('ping',['-c','100','127.0.0.1']),str = ""; spw.stdout.on('data',function (data) { str += data.toString(); // just so we can see the server is doing something console.log("data"); // Flush out line by line. var lines = str.split("\n"); for(var i in lines) { if(i == lines.length - 1) { str = lines[i]; } else{ // Note: The double-newline is *required* res.write('data: ' + lines[i] + "\n\n"); } } }); spw.on('close',function (code) { res.end(str); }); spw.stderr.on('data',function (data) { res.end('stderr: ' + data); }); }); app.listen(4000);
客户端HTML:
<!DOCTYPE Html> <html> <body> <ul id="eventlist"> </ul> <script> var eventList = document.getElementById("eventlist"); var evtSource = new EventSource("http://localhost:4000/msg"); var newElement = document.createElement("li"); newElement.innerHTML = "Messages:"; eventList.appendChild(newElement); evtSource.onmessage = function(e) { console.log("received event"); console.log(e); var newElement = document.createElement("li"); newElement.innerHTML = "message: " + e.data; eventList.appendChild(newElement); }; evtSource.onerror = function(e) { console.log("EventSource Failed."); }; console.log(evtSource); </script> </body> </html>
运行节点index.js并将浏览器指向http:// localhost:4000 / client.html.请注意,自从我运行OS X以来,我不得不使用“-c”选项而不是“-n”.
node.js – NodeJs:二进制fs.createReadStream流式传输为UTF-8
我的代码是(简化; res是SeverResponse类型):
... var fs = require('fs'); var stream = fs.createReadStream(pathToJPEG,{encoding: 'binary'}); res.setHeader('Content-Type',"image/jpeg"); stream.pipe(res); ...
事实证明,到达浏览器的是源数据的UTF-8编码版本.我也能够将响应对象排除在罪魁祸首之外.当我给它一个替代流(从缓冲区,而不是文件)它工作得很好.
事实证明我的问题的解决方案是删除选项{encoding:’binary’}.我的浏览器收到了正确的图片:
... var fs = require('fs'); var stream = fs.createReadStream(pathToJPEG); res.setHeader('Content-Type',"image/jpeg"); stream.pipe(res); ...
我的问题是:为什么?
看起来很直观,第一个非工作版本应该是正确的,因为它明确声明了如何读取文件.
解决方法
‘binary’ – A way of encoding the buffer into a one-byte (i.e. latin-1) encoded string. The string ‘latin-1’ is not supported. Instead simply pass ‘binary’ to use ‘latin-1’ encoding.
只需将编码设置为null即可获取原始流或缓冲区,或者根本不像第二个示例中那样指定任何内容.
node.js – util.pump(streamA,streamB)和streamA.pipe(streamB)之间有什么区别?
pump
,pipe
.为什么我要使用一个而不是另一个?另一个只是一个更好的版本吗?
解决方法
https://groups.google.com/forum/?fromgroups#!topic/nodejs/FwdDQvAf4xM
https://stackoverflow.com/a/4591335/424851
node.js – 如何测试对象是否是NodeJS中的Stream
我可以很容易地测试对象是否是这样的缓冲区:
if(x instanceof Buffer)
测试对象是否为Stream的最佳方式是什么?节点中似乎没有Stream基类 – 是吗?
我该怎么办?
解决方法
今天的关于node.js中 stream使用教程和nodejs stream原理的分享已经结束,谢谢您的关注,如果想了解更多关于node.js – nodejs / express-stream stdout立即向客户端发送、node.js – NodeJs:二进制fs.createReadStream流式传输为UTF-8、node.js – util.pump(streamA,streamB)和streamA.pipe(streamB)之间有什么区别?、node.js – 如何测试对象是否是NodeJS中的Stream的相关知识,请在本站进行查询。
本文标签: