http / 2推送javascript没有加载

时间:2017-07-22 05:46:20

标签: node.js express http2

我正在使用spdy nodejs库在请求进入时下载我的javascript包。

spdy
    .createServer(options, (req, res) => {
        // TODO - figure out why the javascript doesnt load on the client
        if (res.push) {
            console.log("Pushing...");
            // push vendor bundle
            const bundleStream = res.push(`/js/client.js`, {
                status: 200,
                method: "GET",
                request: { accept: "*/*" },
                response: { "content-type": "application/javascript" },
            });

            bundleStream.on("error", (e: any) => console.error(e));
            bundleStream.end();
            // push main bundle
            const vendorStream = res.push(`/js/vendor.js`, {
                status: 200,
                method: "GET",
                request: { accept: "*/*" },
                response: { "content-type": "application/javascript" },
            });
            vendorStream.on("error", (e: any) => console.error(e));
            vendorStream.end();
        }
        return app(req as any, res as any);
    })
    .listen(port, (err: any) => {

其中app是快速js中间件。如果它不是https服务器,一切正常,但是一旦配置了https,推送就会显示在请求中,但内容为空(没有执行javascript)

push request showing up

看起来这些文件的内容为零,但我知道服务器知道如何提供它们,因为它在未启用推送时正确执行。

这让我疯狂,所以任何见解都会受到赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

致电时:

bundleStream.end();

和:

vendorStream.end();

您已将文件输出传递给response.end()方法,如果您未指定该方法,则流无法识别加载文件所需的位置。因此,您对end的致电应该是这样的:

bundleStream.end(fs.readFileSync('path-to-bundle-file', 'utf-8'));
vendorStream.end(fs.readFileSync('path-to-vendor-file', 'utf-8'));