Có thể tải máy chủ HTTP/2 (HTTP 2.0) của node.js không? Và phiên bản http 2.0 của express.js?máy chủ node.js và HTTP/2 (2.0) với express.js
Trả lời
Nếu bạn đang sử dụng [email protected]^5
và [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 express
và http2
để 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 */
});
Đ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
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.
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 –
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
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 :)
- 1. HAProxy với frontend HTTP2 và HTTP1.1 backend
- 2. Học node.js/express.js: Thỏa thuận với bin/www là gì?
- 3. Trò chơi máy khách và máy chủ Node.js bằng Telnet
- 4. Hiển thị bên máy chủ Express.js - Yêu cầu '/ json/version/
- 5. Sử dụng Node.JS làm máy chủ REST và máy chủ web
- 6. Móc tắt máy Express.js
- 7. Tách logic máy chủ tệp và socket.io trong node.js
- 8. Đọc tệp từ máy chủ SFTP bằng Node.js và SSH2
- 9. Cách chạy máy chủ được viết bằng js với Node.js
- 10. Sự khác biệt phông chữ của Node.js & Express.js
- 11. Tải xuống tệp từ máy chủ Node.JS với AngularJS
- 12. Đo và giám sát Node.JS hiệu suất máy chủ
- 13. SSL bắt tay faillure với máy chủ node.js
- 14. Triển khai máy chủ Node.js sản xuất
- 15. Triển khai máy chủ HTTP RxJS + node.js?
- 16. Chạy SSL Node.js máy chủ với gd_bundle.crt GoDaddy
- 17. Tại sao cài đặt máy chủ (Nginx, Apache ...) với Node.js?
- 18. Gỡ lỗi rò rỉ bộ nhớ với máy chủ Node.js
- 19. Lưu trữ ảo với máy chủ node.js độc lập
- 20. Bắt đầu một máy chủ node.js
- 21. Tệp POST của Node.js tới Máy chủ
- 22. Bảo vệ mã phía máy chủ Node.js
- 23. Cách bật http2 trong Windows 10 máy chủ kỹ thuật xem trước
- 24. Xuất bản máy chủ Node.JS trên Internet
- 25. Máy chủ ủy quyền HTTPS trong node.js
- 26. Node.js - máy chủ đã đóng kết nối?
- 27. Express.js trên Node.js có bị rò rỉ bộ nhớ không?
- 28. Bất kỳ khung JavaScript nào của phía máy khách có tích hợp tốt với Node.js + Express.js + socket.io.js không?
- 29. làm cách nào để gửi lại biểu mẫu bài đăng đa dạng/biểu mẫu dữ liệu với tệp tải lên lên máy chủ khác với node.js (express.js)?
- 30. Chứng chỉ chuỗi Node.js/Express.js Không hoạt động
Theo họ, họ đã ủng hộ nó. https://github.com/strongloop/express/issues/2364 –