Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用spawn函数处理静态htm_Javascript_Node.js_Stream_Pipe_Spawn - Fatal编程技术网

Javascript 使用spawn函数处理静态htm

Javascript 使用spawn函数处理静态htm,javascript,node.js,stream,pipe,spawn,Javascript,Node.js,Stream,Pipe,Spawn,我尝试编写web服务器以进行调试。 在发送响应之前,它使用gpp(通用预处理器)处理静态html文件 我使用由我修补的“发送”npm包。最初,使用HTTP头“发送”发送支持它们的静态文件。 我将流数组添加到原始模块中,以转换请求的静态文件 // pipe var stream = fs.createReadStream(path, options); this.emit('stream', stream); var sm = stream; if (transforms

我尝试编写web服务器以进行调试。 在发送响应之前,它使用gpp(通用预处理器)处理静态html文件

我使用由我修补的“发送”npm包。最初,使用HTTP头“发送”发送支持它们的静态文件。 我将流数组添加到原始模块中,以转换请求的静态文件

   // pipe
  var stream = fs.createReadStream(path, options);
  this.emit('stream', stream);

  var sm = stream;

  if (transforms instanceof Array) {
      for (var i=0; i<transforms.length; i++) {
          sm = sm.pipe(transforms[i]);
      }
  }

  sm.pipe(res);
问题在于:html页面没有完全加载到浏览器中。

我的html页面源代码(我省略了不必要的工作人员):

工作正常(页面按原样加载)

如果我在发送模块中更改

sm.pipe(res);

工作正常(已处理的页面加载到文件中)

如果我这样做:

cat app/index-debug.html | gpp -Iapp
它工作正常(已处理的页面位于标准输出上)

我想在流同步中有一个问题。
但是我找不到它。

我找到了问题的原因。这是由于响应的内容长度设置为原始文件的大小,而不是转换文件的大小。我会尽快描述一个修复。不幸的是,这不可能用流包装gpp。我必须在发送任何数据块之前设置内容长度。我的解决方案是将gpp调用包含到发送模块中。
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="app" ng-controller="AppCtrl">
<head>
#include "scripts-debug.tpl.html"
</head>
<body>
#include "body.tpl.html"
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="app" ng-controller="AppCtrl">
<head>
<!-- scripts-debug.tpl.html -->
<script type="text/javascript" src="app/app.js"></script>
<script type="text/javascript" src="app/con
var gpp = spawn( 'gpp', [ '-Iapp' ]);
var gpp = spawn( 'grep', [ '.' ]);
sm.pipe(res);
sm.pipe( fs.createWriteStream( 'test.dst' ));
cat app/index-debug.html | gpp -Iapp