问题描述
试图构建一个节点服务器,以交流进度和最终结果以卷曲调用和节点生成
在Node JS服务器上
Http.createServer((Req,Res)=>{ //...etc set up Node Server
if(Req.url=="/TestProgress") TestProgress(Res); //Respond to API call
function TestProgress(Req,Res){ //Process request
Res.setHeader('Connection','transfer-encoding');
Res.setHeader('Content-Type','text/plain; charset=utf-8');
Res.setHeader('transfer-encoding','chunked');
var Count=0;
var Intvl=setInterval(()=>{
Count++;
ThisReturn="In Progress - " + Count;
Res.write(ThisReturn + "\n");
if(Count==10){
clearInterval(Intvl);
Res.end();
}
},300);
}
从命令行
> curl 'https://ServerAddress.com/TestProgress
结果实时填充。每300毫秒-与上面的setInterval循环的持续时间相对应...
In Progress - 1
In Progress - 2
...etc...
In Progress - 10
我通过节点生成尝试了相同的调用,结果出乎意料。 (下面的评论)
thisspawn=require("child_process").spawn("curl",["https://ServerAddress.com/TestProgress"]);
thisspawn.stdout.on("data",ThisData=>{
console.log(ThisData.toString());
//Expectation: This would trigger with each "Res.write()"
//similar to the cURL call in command line.
//Instead it returns the final StdOut on completion.
});
thisspawn.stderr.on("data",ThisData=>{
console.log("StdErr",ThisData.toString());
//Returns a standard curl progress,//e.g. "Dload Upload Total Spent"
//irrespective of the interval duration set the Node Server.
});
thisspawn.on("close",ThisData=>{
console.log(ThisData);
//Outputs: 0
});
在命令行中,每次节点服务器发出Res.write()
(以换行符“ \ n”结尾)时,cURL调用都将输出stdout,从而生成进度报告样式输出
但是从 Node / Spawn 中,相同的cURL调用不会。
好奇是否有人对幕后发生的原因/发生什么有所了解。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)