Ответ 1
Если вы используете [email protected]^5
и [email protected]^3.3.4
, то правильный способ запуска сервера:
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 */
});
Обратите внимание на https2.raw
. Это необходимо, если вы хотите принимать TCP-соединения.
Обратите внимание, что на момент написания этой статьи (2016 05 06) ни один из основных браузеров не поддерживает HTTP2 через TCP.
Если вы хотите принимать соединения TCP и TLS, вам необходимо запустить сервер, используя метод createServer
по умолчанию:
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 */
});
Обратите внимание, что на момент написания этой статьи мне удавалось работать express
и http2
(см. https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055). Однако мне удалось получить http2 (и SPDY) для работы с помощью 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 */
});