Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Express http 2 server push

I've made a small server to try http2 on node, however I can't tell if the push mechanism is working or not. My style is loaded over http2 but that does not mean that push is working as intended...

const port = 3000
const spdy = require('spdy')
const express = require('express')
const path = require('path')
const fs = require('fs')
const app = express()

app.get('*', (req, res) => {
    res.set('Link', '</css/styles.css>; rel=preload; as=style');
  res.sendFile(__dirname + '/index.html')
})

const options = {
    key: fs.readFileSync(__dirname + '/keys/server.key'),
    cert:  fs.readFileSync(__dirname + '/keys/server.crt')
}

spdy.createServer(options, app).listen(3000);

In the devtools, the initiator says : "other".

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to explicitly tell your express server which resources to push. Look here for a decent run through of it:

https://webapplog.com/http2-server-push-node-express/

An example route from that page:

app.get('/pushy', (req, res) => {
  var stream = res.push('/main.js', {
    status: 200, // optional
    method: 'GET', // optional
    request: {
      accept: '*/*'
    },
    response: {
      'content-type': 'application/javascript'
    }
  })
  stream.on('error', function() {
  })
  stream.end('alert("hello from push stream!");')
  res.end('<script src="/main.js"></script>')
})

Calling res.push() is the key here with the file you want to serve.

He also documents a way to write some middleware to handle pushing as well of assets:

https://webapplog.com/http2-server-push-express-middleware/

Once you have this implemented you can see the pushed items in Chrome devtools as in the answer from BazzaDP.

about 4 years ago · Santiago Trujillo Relatório

0

Chrome will show "Push / Other" in the Initiator column in Developer Tools->Network if the asset is pushed from the server:

enter image description here

I don't use the SDPY module in Node, but looks like from here and here that this module doesn't use the Link header to push resources like some other servers (e.g. Apache) do. So unless you have, for example, Apache in front of this in HTTP/2 mode then I doubt your code will push the resource.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda