管道数据块作为对客户端终端的响应
Pipe data chunks as a Response to a clients terminal
所以我的问题是关于 Node js 管道的。所以我的后端看起来像这样——有一个简单的路由,路由调用函数并将可执行类型文件的文件路径传递给它。这个文件然后是 运行 和 childProcess.spawn
并且有一个 data
输出,我可以 console.log
const express = require("express");
const app = express();
etc...
const runExecutable = (executableFile) => {
const runFile = childProcess.spawn(executableFile);
runFile.stdout.on('data', function(data){
console.log("DATA", data);
})
runFile.on('exit', function(code, signal){
[some code here]
})
}
app.get('/example', (req, res) => {
var file = "./testFile.exe";
runExecutable(file);
})
我的问题是如何将 data
/a.k.a 块的输出实时传输到客户端,对他们来说重要的是在数据出来时获取数据而不是让我把它写到一个文件里,然后把整个东西发给他们。还需要注意的一件事是,客户端正在通过他们终端中的 curl curl 123.45.678.901/example
访问我的路由,我想将 data
通过管道传输到他们的终端。
在四处阅读时,我知道例如请求模块执行 request.get(url).pipe(res) /[Express res]
,所以我想知道这是否与我可能需要做的类似。
谢谢大家!
找到答案:任何流都可以通过管道传输 - readable.pipe(destination[, options]) - childProcess.spawn(executableFile)
不是流,但是一旦文件开始执行,它就会发出一个 "data"
事件,它是另一种说法是从文件的 运行 发出了一个流。因此,如果您正在查看 "data"
的这些大块内容 - 就像我一样 - 就像这样:
runFile.stdout.on('data', function(data){
console.log("DATA", data);
})
那就是你使用的流,也是你附加 pipe
的地方
Node 文档基本上说 - 附加到流 .pipe
然后将它发送到它的目的地。因为我想将这些数据块发送给我的客户端,所以我还必须传递 res
,所以我的代码现在看起来像这样:
const express = require("express");
const app = express();
etc...
const runExecutable = (executableFile, res) => {
const runFile = childProcess.spawn(executableFile);
runFile.stdout.on('data', function(data){
console.log("DATA", data);
}).pipe(res)
runFile.on('exit', function(code, signal){
[some code here]
})
}
app.get('/example', (req, res) => {
var file = "./testFile.exe";
runExecutable(file, res);
})
而且有效!我希望这对其他人有帮助 - 感谢李的帮助!
所以我的问题是关于 Node js 管道的。所以我的后端看起来像这样——有一个简单的路由,路由调用函数并将可执行类型文件的文件路径传递给它。这个文件然后是 运行 和 childProcess.spawn
并且有一个 data
输出,我可以 console.log
const express = require("express");
const app = express();
etc...
const runExecutable = (executableFile) => {
const runFile = childProcess.spawn(executableFile);
runFile.stdout.on('data', function(data){
console.log("DATA", data);
})
runFile.on('exit', function(code, signal){
[some code here]
})
}
app.get('/example', (req, res) => {
var file = "./testFile.exe";
runExecutable(file);
})
我的问题是如何将 data
/a.k.a 块的输出实时传输到客户端,对他们来说重要的是在数据出来时获取数据而不是让我把它写到一个文件里,然后把整个东西发给他们。还需要注意的一件事是,客户端正在通过他们终端中的 curl curl 123.45.678.901/example
访问我的路由,我想将 data
通过管道传输到他们的终端。
在四处阅读时,我知道例如请求模块执行 request.get(url).pipe(res) /[Express res]
,所以我想知道这是否与我可能需要做的类似。
谢谢大家!
找到答案:任何流都可以通过管道传输 - readable.pipe(destination[, options]) - childProcess.spawn(executableFile)
不是流,但是一旦文件开始执行,它就会发出一个 "data"
事件,它是另一种说法是从文件的 运行 发出了一个流。因此,如果您正在查看 "data"
的这些大块内容 - 就像我一样 - 就像这样:
runFile.stdout.on('data', function(data){
console.log("DATA", data);
})
那就是你使用的流,也是你附加 pipe
Node 文档基本上说 - 附加到流 .pipe
然后将它发送到它的目的地。因为我想将这些数据块发送给我的客户端,所以我还必须传递 res
,所以我的代码现在看起来像这样:
const express = require("express");
const app = express();
etc...
const runExecutable = (executableFile, res) => {
const runFile = childProcess.spawn(executableFile);
runFile.stdout.on('data', function(data){
console.log("DATA", data);
}).pipe(res)
runFile.on('exit', function(code, signal){
[some code here]
})
}
app.get('/example', (req, res) => {
var file = "./testFile.exe";
runExecutable(file, res);
})
而且有效!我希望这对其他人有帮助 - 感谢李的帮助!