Importante

Você está vendo a versão anterior da nova experiência da Alura que estamos preparando para você. Em breve, ela ganha uma identidade visual novinha totalmente pensada em potencializar seus estudos!

0
respostas

Biblioteca antiga spdy

Estou utilizando a versão v24.18.0 do nodejs e ele não aceita a biblioteca do spdy.

Por isso coloquei no conteúdo do arquivo server_http2.js as seguintes instruções.

const http2 = require('http2');
const fs = require('fs');
const path = require('path');

const server = http2.createSecureServer({
    key: fs.readFileSync('./server.key'),
    cert: fs.readFileSync('./server.crt')
});

server.on('error', (err) => console.error(err));

server.on('stream', (stream, headers) => {
    let reqPath = headers[':path'];
    
    if (reqPath === '/') {
        reqPath = '/index.html';
    }

    const filePath = path.join(__dirname, 'build', reqPath);

    fs.readFile(filePath, (err, data) => {
        if (err) {
            stream.respond({ ':status': 404, 'content-type': 'text/plain' });
            stream.end('Not Found');
            return;
        }

        let contentType = 'text/html';
        if (filePath.endsWith('.js')) contentType = 'application/javascript';
        else if (filePath.endsWith('.css')) contentType = 'text/css';
        else if (filePath.endsWith('.png')) contentType = 'image/png';
        else if (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg')) contentType = 'image/jpeg';
        else if (filePath.endsWith('.json')) contentType = 'application/json';

        stream.respond({
            ':status': 200,
            'content-type': contentType
        });
        stream.end(data);
    });
});

server.listen(3002, '0.0.0.0', () => {
    console.log('Listening on port 3002 with Native HTTP/2');
});