2015-02-20 15 views

Odpowiedz

10

Jeśli używasz [email protected]^5 i [email protected]^3.3.4, to poprawny sposób, aby uruchomić serwer jest:

const http2 = require('http2'); 
const express = require('express'); 

const app = express(); 

// app.use('/', ..); 

http2 
    .raw 
    .createServer(app) 
    .listen(8000, (err) => { 
     if (err) { 
      throw new Error(err); 
     } 

     /* eslint-disable no-console */ 
     console.log('Listening on port: ' + argv.port + '.'); 
     /* eslint-enable no-console */ 
    }); 

Zwróć uwagę na https2.raw. This is required if you want to accept TCP connections.

Należy pamiętać, że w momencie pisania tego artykułu (2016 05 06), none of the major browsers support HTTP2 over TCP.

Jeśli chcesz akceptować połączenia TCP i TLS, to trzeba uruchomić serwer używając createServer Metoda domyślna:

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


const app = express(); 

// app.use('/', ..); 

http2 
    .createServer({ 
     key: fs.readFileSync('./localhost.key'), 
     cert: fs.readFileSync('./localhost.crt') 
    }, app) 
    .listen(8000, (err) => { 
     if (err) { 
      throw new Error(err); 
     } 

     /* eslint-disable no-console */ 
     console.log('Listening on port: ' + argv.port + '.'); 
     /* eslint-enable no-console */ 
    }); 

Należy pamiętać, że w momencie pisania tego tekstu, że udało się zrobić express i http2 do pracy (patrz https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055). Jednak udało mi się uzyskać http2 (i SPDY) do pracy przy użyciu pakietu spdy.

const spdy = require('spdy'); 
const express = require('express'); 
const path = require('path'); 
const fs = require('fs'); 

const app = express(); 

app.get('/', (req, res) => { 
    res.json({foo: 'test'}); 
}); 

spdy 
    .createServer({ 
     key: fs.readFileSync(path.resolve(__dirname, './localhost.key')), 
     cert: fs.readFileSync(path.resolve(__dirname, './localhost.crt')) 
    }, app) 
    .listen(8000, (err) => { 
     if (err) { 
      throw new Error(err); 
     } 

     /* eslint-disable no-console */ 
     console.log('Listening on port: ' + argv.port + '.'); 
     /* eslint-enable no-console */ 
    }); 
+1

Co ciekawe, gdy użyłem tego i https://github.com/expressjs/express/issues/2761#issuecomment-216912022 pojawia się ten błąd. Ostrzeżenie (węzeł): możliwe wykrycie wycieku pamięci EventEmitter. Dodano 11 słuchaczy błędów. Użyj opcji emitter.setMaxListeners(), aby zwiększyć limit. – zmanc

25
var express = require('express'); 
var app = express(); 

app.get('/', function (req, res) { 
    res.send('hello, http2!'); 
}); 

var options = { 
    key: fs.readFileSync('./example/localhost.key'), 
    cert: fs.readFileSync('./example/localhost.crt') 
}; 

require('http2').createServer(options, app).listen(8080); 

EDIT

Kod ten fragment został zaczerpnięty z a conversation on Github.

+9

FYI To nie działa z 'express @ 4.13.3' i' http2 @ 3.2.0', i wygląda na to, że express nie będzie go obsługiwał dopóki v5. https://github.com/molnarg/node-http2/issues/100 –

+0

Nie działa dla mnie z 'node @ v6.7.0',' express @ 5.0.0-alpha.2', '[email protected] 6'. _TypeError: dest.end nie jest function_ – neoDev

0

Ten problem jest nadal około dzisiaj (ponad rok później), więc zdecydowaliśmy się iść na celu obejście aby express i http2 pakiety działają dobrze razem. Stworzyłem pakiet npm, który robi dokładnie to: https://www.npmjs.com/package/express-http2-workaround

zainstalować poprzez KMP: npm zainstalować Express http2-obejście flagą --save

// Require Modules 
var fs = require('fs'); 
var express = require('express'); 
var http = require('http'); 
var http2 = require('http2'); 

// Create Express Application 
var app = express(); 

// Make HTTP2 work with Express (this must be before any other middleware) 
require('express-http2-workaround')({ express:express, http2:http2, app:app }); 

// Setup HTTP/1.x Server 
var httpServer = http.Server(app); 
httpServer.listen(80,function(){ 
    console.log("Express HTTP/1 server started"); 
}); 

// Setup HTTP/2 Server 
var httpsOptions = { 
    'key' : fs.readFileSync(__dirname + '/keys/ssl.key'), 
    'cert' : fs.readFileSync(__dirname + '/keys/ssl.crt'), 
    'ca' : fs.readFileSync(__dirname + '/keys/ssl.crt') 
}; 
var http2Server = http2.createServer(httpsOptions,app); 
http2Server.listen(443,function(){ 
    console.log("Express HTTP/2 server started"); 
}); 

// Serve some content 
app.get('/', function(req,res){ 
    res.send('Hello World! Via HTTP '+req.httpVersion); 
}); 

Powyższy kod jest aplikacją, która działa ekspresowa wykorzystuje zarówno moduł http nodejs (dla HTTP/1.x) i moduł http2 (dla HTTP/2).

Jak wspomniano w pliku Readme, tworzy on nowe, wyraźne obiekty żądania i odpowiedzi oraz ustawia ich prototypy na obiekty IncomingMessage i ServerResponse. Domyślnie jest to wbudowane nodejs w obiektach http IncomingMessage i ServerResponse.

Mam nadzieję, że to pomoże :)

Powiązane problemy