【问题标题】:Nodejs - SPDY push on request errorNodejs - SPDY 推送请求错误
【发布时间】:2014-03-16 12:37:54
【问题描述】:

我无法启动我的 Node 应用程序。我收到“TypeError:无法调用未定义的方法‘push’”错误。 这是我的代码:

spdy.createServer(credentials, app).listen(app.get('port'), function(req, res) {
 res.push('public/js/bootstrap.min.js', {'content-type': 'application/javascript'}, function(err, stream) {
        stream.end('alert("hello from push stream!")');
      });
 res.push('public/js/jquery.min.js', {'content-type': 'application/javascript'}, function(err, stream) {
        stream.end();
      });
 res.push('public/css/bootstrap.min.css', {'content-type': 'text/css'}, function(err, stream) {
        stream.end();
      });

      // write main response body and terminate stream
      res.end('Hello World!');

console.log('Express HTTPS SPDY server listening on port ' + app.get('port'));
}   );

我正在运行 SDPY 版本 1.19.2 和 NodeJS 版本 0.10.25。

【问题讨论】:

    标签: node.js spdy


    【解决方案1】:

    根据您的comment,您使用的是 HTTP/1.1。您需要使用 HTTP2/SPDY 服务才能进行推送。

    【讨论】:

      【解决方案2】:

      试试这个:

      // set the root path of Express server
      app.use(express.static(__dirname+'/public');
      
      app.use(function(req, res) {
        // your res.push code is here.
        var stream = res.push('/js/bootstrap.min.js', {'content-type': 'application/javascript'});
        stream.on('acknowledge', function() {
          console.log('stream ACK');
        });
        stream.on('error', function() {
          console.log('stream ERR');
        });
        stream.end('alert("stream SUCCESSFUL");');
        res.end('<script src="/js/bootstrap.min.js"></script>');
      });
      
      spdy.createServer(credentials, app).listen(app.get('port'));
      

      传递给listen() 函数的回调函数不带任何参数。见第 49 行,文件 stream-test.js

      【讨论】:

      • 错误已停止出现。但是当我查看 Firefox 27 的网络检查器时,我仍然看到在 HTML 文件之后获取的文件。我正在尝试让它们同时下载。
      • 服务器返回的响应是什么?
      • 内容长度:852 内容类型:文本/html; charset=utf-8 etag:"-1294749726" 状态:200 OK 版本:HTTP/1.1 x-powered-by:Express
      • 我已经更新了代码。它现在应该可以工作了。此代码已使用 Firefox 27 和 Chrome 32 进行测试。
      • 我刚刚用 Firefox 27 和 Chrome 32 进行了测试,但没有成功。没有日志显示,也没有警报消息。
      猜你喜欢
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多