2015-02-20 15 views

Trả lời

10

Nếu bạn đang sử dụng [email protected]^5[email protected]^3.3.4, sau đó cách chính xác để khởi động server là:

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 */ 
    }); 

Hãy chú ý đến số https2.raw. This is required if you want to accept TCP connections.

Lưu ý rằng tại thời điểm viết bài này (2016 05 06), none of the major browsers support HTTP2 over TCP.

Nếu bạn muốn chấp nhận kết nối TCP và TLS, thì bạn cần phải khởi động máy chủ sử dụng createServer phương pháp mặc định:

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 */ 
    }); 

Lưu ý rằng tại thời điểm viết bài này, tôi đã quản lý để làm expresshttp2 để hoạt động (xem https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055). Tuy nhiên, tôi đã quản lý để có được http2 (và SPDY) để làm việc bằng cách sử dụng gói 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

Điều thú vị là đủ khi tôi sử dụng và https://github.com/expressjs/express/issues/2761#issuecomment-216912022 Tôi gặp phải lỗi này. (nút) cảnh báo: có thể phát hiện rò rỉ bộ nhớ EventEmitter. Đã thêm 11 trình nghe lỗi. Sử dụng emitter.setMaxListeners() để tăng giới hạn. – 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

Đoạn mã này được lấy từ a conversation on Github.

+9

FYI Thao tác này không hoạt động với 'express @ 4.13.3' và' http2 @ 3.2.0', và có vẻ như Express sẽ không hỗ trợ cho đến khi v5. https://github.com/molnarg/node-http2/issues/100 –

+0

Nó không hoạt động với tôi với 'node @ v6.7.0',' express @ 5.0.0-alpha.2', '[email protected] 6'. _TypeError: dest.end không phải là hàm_ – neoDev

0

Vấn đề này vẫn còn khoảng ngày hôm nay (hơn một năm sau), vì vậy tôi quyết định giải quyết để làm cho các gói express và http2 hoạt động tốt với nhau. Tôi đã tạo ra một gói NPM thực hiện chính xác rằng: https://www.npmjs.com/package/express-http2-workaround

Cài đặt qua NPM: NPM cài đặt nhanh-http2-workaround --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); 
}); 

Đoạn mã trên là một ứng dụng nhanh làm việc có sử dụng cả module http nodejs (cho HTTP/1.x) và mô-đun http2 (cho HTTP/2).

Như đã đề cập trong readme, điều này tạo ra các đối tượng yêu cầu và phản hồi nhanh mới và đặt nguyên mẫu của chúng thành đối tượng IncomingMessage và ServerResponse của http2. Theo mặc định, đó là các đối tượng trong các đối tượng InboundMessage và ServerResponse.

Tôi hy vọng điều này sẽ giúp :)

Các vấn đề liên quan