如何读取一个角流的NodeJS? [英] How to read a nodejs stream in angular?
问题描述
在我的NodeJS发送一个流给客户这样的:
In nodejs I send a stream to clients like this:
DockerService.get().pull('my/image', function(err, stream) {
if (err) {
return res.badRequest(err);
} else {
return stream.pipe(res);
}
});
这是从angularjs客户端使用引发的 $ http.get
This is triggered from a angularjs client using $http.get:
pull: function() {
var url = ENV.apiEndpoint + '/docker/pull';
var promise = $http.get(url).success(function(response) {
return response;
}).error(function(data, status) {
});
return promise;
}
不过,我不能读取流。从我的角度得到一个错误信息:
However I cannot read the stream. From angular I get an error message:
SyntaxError: Unexpected token {
at Object.parse (native)
at fromJson (file://build/vendor/angular/angular.js:1065:14)
at defaultHttpResponseTransform (file://build/vendor/angular/angular.js:8579:16)
at file://build/vendor/angular/angular.js:8664:12
at forEach (file://build/vendor/angular/angular.js:323:20)
at transformData (file://build/vendor/angular/angular.js:8663:3)
at transformResponse (file://build/vendor/angular/angular.js:9389:23)
at processQueue (file://build/vendor/angular/angular.js:13189:27)
at file://build/vendor/angular/angular.js:13205:27
at Scope.$get.Scope.$eval (file://build/vendor/angular/angular.js:14401:28)
我找不到如何在角处理数据流的任何信息。你能帮帮我吗?
I cannot find any information on how to handle streams in angular. Can you help me out?
标题:
响应太大,present在这里,所以这里是它的第一部分:
The response is too large to present it here, so here is the first part of it:
preVIEW:
推荐答案
所以,这不是有效的JSON,这是一个系列的新行分隔(presumably)JSON消息。这是什么意外的标记{的意思。棱角分明的内置解析器不支持开箱即用。现在的问题是不是真正的流媒体与缓存的数据,它只是一个正文格式angularjs不直接支持。你需要确保角度的内置解析器绕过,然后你可以在换行分割字符串和try / catch每行解析作为JSON对象。
So that's not valid JSON, it's a series of newline-delimited (presumably) JSON messages. That's what the "unexpected token '{'" means. Angular's built-in parser doesn't support that out of the box. The question isn't really about streaming vs. buffered data, it's just a body format angularjs doesn't directly support. You'll need to ensure angular's built-in parser is bypassed and then you can split the string on newline and try/catch parsing each line as a JSON object.
FYI我的知识源源不断地进入浏览器的最佳模块是 oboe.js 。您可以使用再加上一些code RonB /角双簧管以每个缓冲然后线解析ndjson语法成JS对象流
FYI to my knowledge the best module for streaming into the browser is oboe.js. You may be able to use RonB/angular-oboe coupled with some code to buffer each line then parse the ndjson syntax into a stream of JS objects.
这篇关于如何读取一个角流的NodeJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!