HTTP2 push with node js?

Hey All,

I am building a progressive web app and would like to push my resources from server to client using node js.

As far as I can tell I have to specify the push on the server side with a reference to the resource in html on client. I originally was going to use rel=preload which would call the push but this is not supported by some browsers.

I’m doing this:

router.get('/streamFile', function (req: any, res: any) {
    ///Push 
    var isSSL = (req.socket.encrypted ? true : false);
    if (isSSL) {
        var index_stream = res.push('/index.js', {
            status: 200, // optional
            method: 'GET', // optional
            request: {
                accept: '*/*'
            },
            response: {
                'content-type': 'application/javascript'
            }
        })
        var vendor_stream = res.push('/vendor.js', {
            status: 200, // optional
            method: 'GET', // optional
            request: {
                accept: '*/*'
            },
            response: {
                'content-type': 'application/javascript'
            }
        })
        let index_file = fs.createReadStream('./dist/assets/bundle/index.js');
        let vendor_file = fs.createReadStream('./dist/assets/bundle/vendor.bundle.js');
        vendor_file.pipe(vendor_stream);
        index_file.pipe(index_stream);

        //stream.end('alert("hello from push stream!")');
        //console.log("Encrypted!!!!!!!!!!!")
    }
    res.writeHead(200);
    res.end('<div id="react-contact"></div> <script src="/vendor.js"></script> <script src="/index.js"></script>');
}); 

but don’t know if this is the best way? How are you doing it? If your not using http2 in this way then why?

thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.