spdy-http2 / node-spdy

SPDY server on Node.js
2.81k stars 196 forks source link

Using Express and res.push() #223

Closed mavrick closed 8 years ago

mavrick commented 8 years ago

I'm trying to figure out the proper implementation of using push streams inside the express middleware callback.

var fs = require('fs');
var spdy = require('spdy');
var express = require("express");
var app = express();

var https = require('https');
var http = require('http');

var fs = require('fs');
var jqueryCache = fs.readFileSync(__dirname + '/jquery.js');

app.use(function(req, res, next) 
{
    if(req.url == '/jquery.js')
    {
        var stream = res.push('/jquery.js', {response:{'content-type': 'application/javascript'}});

        stream.on('acknowledge', function() {});
        stream.on('error', function() {});

        stream.end(jqueryCache.toString('utf8'));

        res.end('<script src="/jquery.js"></script>');
        return;
    }

    next();
});

var key = fs.readFileSync(__dirname + '/website.key');
var crt = fs.readFileSync(__dirname + '/website.crt');
var ca  = fs.readFileSync(__dirname + '/website.ca-bundle.crt');

var credentials = {
    key: key.toString(),
    cert: crt.toString(),
    ca: ca.toString()
}

http.createServer(app).listen(80);
spdy.createServer(credentials, app).listen(443);

How to use push streams from a SPA still feels a bit vague from the documentation.

Do I still include the <script src="/jquery.js"></script> within the HTML or can it be left out now?

Thanks in advance

indutny commented 8 years ago

Heya!

You need to include the file on the client-side. Pushes are basically writing the data to the browser's cache, so when it will see src="/jquery.js" - browser will load the pushed response.

jcheenatcode commented 6 years ago

@mavrick / @indutny Does this line work "res.push? Since that app is an express app, its giving me Property 'Push' does not exist on type response. Was there a way I can get it to work?