节点 HTTP/2 ERR_CONNECTION_REFUSED
NodeJS HTTP/2 ERR_CONNECTION_REFUSED
我正在尝试使用 molnarg 的 node-http2 模块在节点服务器上实现 HTTP/2。我原来的 server.js 工作正常,当我实现 HTTP/2 模块时,我得到一个
ERR_CONNECTION_REFUSED
来自浏览器(在 Chrome 图书上使用 Chrome)。
要实施的更改列表 HTTP/2:
- 创建了证书和密钥 .perm
- 将 config.json 中的
hostPort
变量更改为 443(已尝试 w/80、8000)
添加到server.js:
var certs = {key: fs.readFileSync('../../key.pem'),cert: fs.readFileSync('../../cert.pem')};
编辑server.js
require('http').createServer(function (request, response) {...}
到
require('http2').createServer(certs, function (request, response) {...}
我遗漏了什么或有什么错误(我在日志中没有发现错误)?
下面是server.js
的副本
var environment = '../env/' + process.env.NODE_ENV;
// Process User config
var fS = require('fs')
, jsonFile = fS.readFileSync(environment + '/config.json'), jsonString, hostIp, hostPort, cacheExp, cps;
try {
jsonString = JSON.parse(jsonFile);
var SERV_HOST = jsonString['hostIp']
, SERV_PORT = jsonString['hostPort']
, CACHE_EXP = jsonString['cacheExp']
, CPS = jsonString['cps']
, xPowerBy = ''
, xFrameOptions = ''
, xXSSProtection = ''
, xContentTypeOption = ''
, cacheControl = '';
} catch (err) {
console.log('There is an error parsing the json file : ' + err);
}
// Load required modules
var web = require('node-static')
, watch = require('staticsmith-watch')
, fs = require("fs");
var certs = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
// Load security setings via config.json
var security =
[{
// Just some security stuff here
}];
if (process.env.NODE_ENV == 'production') {
var options = { host: SERV_HOST
, port: SERV_PORT
, cache: CACHE_EXP
};
var public_dir = new web.Server(environment, options);
// Serve it up!
require('http2').createServer(certs, function (request, response) {
// OLD
// require('http').createServer(function (request, response) {
// Add Content Security Rules
for(var i = 0; i < security.length; i++){
response.setHeader(security[i].name, security[i].option);
}
request.addListener('end', function () {
public_dir.serve(request, response, function (err, result) {
if (err) { // There was an error serving the file
console.error("Error serving " + request.url + " - " + err.message);
// Respond to the client
response.writeHead(err.status, err.headers);
response.end();
}
});
}).resume();
}).listen(options.port, options.host);
console.log("serving at http://" + options.host + ":" + options.port);
console.log("On Node v" + process.versions.node);
watch({
pattern: '**/*',
livereload: true,
});
}
node-spdy 的实现被用来代替 node-http2,因为这个模块你可以在 node.js 中创建 HTTP2 / SPDY 服务器,具有自然的 http 模块接口和回退到常规 https(对于浏览器既不支持 HTTP2,也不支持 SPDY。
server.js现在正在实施新模块:
var environment = '../env/' + process.env.NODE_ENV;
// Process User config
var fS = require('fs')
, jsonFile = fS.readFileSync(environment + '/config.json'), jsonString, hostIp, hostPort, cacheExp, cps;
try {
jsonString = JSON.parse(jsonFile);
var SERV_HOST = jsonString['hostIp']
, SERV_PORT = jsonString['hostPort']
, CACHE_EXP = jsonString['cacheExp']
, CPS = jsonString['cps']
, xPowerBy = ':P'
, xFrameOptions = ''
, xXSSProtection = ''
, xContentTypeOption = ''
, cacheControl = '';
} catch (err) {
console.log('There is an error parsing the json file : ' + err);
}
// Load required modules
var fs = require('fs')
, web = require('node-static');
var public_dir = new web.Server(environment + '/_public');
var options = {
key: fs.readFileSync(environment + '/keys/key.pem')
, cert: fs.readFileSync(environment + '/keys/cert.pem')
// SPDY-specific options
, spdy: {
protocols: [ 'h2','spdy/3.1', 'spdy/3', 'spdy/2','http/1.1', 'http/1.0' ],
plain: false,
connection: {
windowSize: 1024 * 1024, // Server's window size
// **optional** if true - server will send 3.1 frames on 3.0 *plain* spdy
autoSpdy31: false
}
}
, host: SERV_HOST
, port: SERV_PORT
, cache: CACHE_EXP
};
// Load security setings via config.json
var security = [
{ name: 'X-Powered-By',
option: xPowerBy }
, { name: 'x-frame-options',
option: xFrameOptions }
, { name: 'X-XSS-Protection',
option: xXSSProtection }
, { name: 'X-Content-Type-Options',
option: xContentTypeOption }
, { name: 'Cache-Control',
option: CACHE_EXP }
, { name: 'Content-Security-Policy',
option: CPS }
, { name: 'server',
option: 'Who knows' }
];
if (process.env.NODE_ENV == 'production') {
require("spdy").createServer(options, function(req, res) {
// Add Content Security Rules
for(var i = 0; i < security.length; i++){
res.setHeader(security[i].name, security[i].option);
}
public_dir.serve(req, res, function (err, result) {
if (err) { // There was an error serving the file
console.error("Error serving " + req.url + " - " + err.message);
// Respond to the client
res.writeHead(err.status, err.headers);
res.end();
}
});
}).listen(options.port, options.host);
console.log("serving at https://" + options.host + ":" + options.port);
console.log("On Node v" + process.versions.node);
}
开箱即用。
我正在尝试使用 molnarg 的 node-http2 模块在节点服务器上实现 HTTP/2。我原来的 server.js 工作正常,当我实现 HTTP/2 模块时,我得到一个
ERR_CONNECTION_REFUSED
来自浏览器(在 Chrome 图书上使用 Chrome)。
要实施的更改列表 HTTP/2:
- 创建了证书和密钥 .perm
- 将 config.json 中的
hostPort
变量更改为 443(已尝试 w/80、8000) 添加到server.js:
var certs = {key: fs.readFileSync('../../key.pem'),cert: fs.readFileSync('../../cert.pem')};
编辑server.js
require('http').createServer(function (request, response) {...}
到
require('http2').createServer(certs, function (request, response) {...}
我遗漏了什么或有什么错误(我在日志中没有发现错误)?
下面是server.js
的副本var environment = '../env/' + process.env.NODE_ENV;
// Process User config
var fS = require('fs')
, jsonFile = fS.readFileSync(environment + '/config.json'), jsonString, hostIp, hostPort, cacheExp, cps;
try {
jsonString = JSON.parse(jsonFile);
var SERV_HOST = jsonString['hostIp']
, SERV_PORT = jsonString['hostPort']
, CACHE_EXP = jsonString['cacheExp']
, CPS = jsonString['cps']
, xPowerBy = ''
, xFrameOptions = ''
, xXSSProtection = ''
, xContentTypeOption = ''
, cacheControl = '';
} catch (err) {
console.log('There is an error parsing the json file : ' + err);
}
// Load required modules
var web = require('node-static')
, watch = require('staticsmith-watch')
, fs = require("fs");
var certs = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
// Load security setings via config.json
var security =
[{
// Just some security stuff here
}];
if (process.env.NODE_ENV == 'production') {
var options = { host: SERV_HOST
, port: SERV_PORT
, cache: CACHE_EXP
};
var public_dir = new web.Server(environment, options);
// Serve it up!
require('http2').createServer(certs, function (request, response) {
// OLD
// require('http').createServer(function (request, response) {
// Add Content Security Rules
for(var i = 0; i < security.length; i++){
response.setHeader(security[i].name, security[i].option);
}
request.addListener('end', function () {
public_dir.serve(request, response, function (err, result) {
if (err) { // There was an error serving the file
console.error("Error serving " + request.url + " - " + err.message);
// Respond to the client
response.writeHead(err.status, err.headers);
response.end();
}
});
}).resume();
}).listen(options.port, options.host);
console.log("serving at http://" + options.host + ":" + options.port);
console.log("On Node v" + process.versions.node);
watch({
pattern: '**/*',
livereload: true,
});
}
node-spdy 的实现被用来代替 node-http2,因为这个模块你可以在 node.js 中创建 HTTP2 / SPDY 服务器,具有自然的 http 模块接口和回退到常规 https(对于浏览器既不支持 HTTP2,也不支持 SPDY。
var environment = '../env/' + process.env.NODE_ENV;
// Process User config
var fS = require('fs')
, jsonFile = fS.readFileSync(environment + '/config.json'), jsonString, hostIp, hostPort, cacheExp, cps;
try {
jsonString = JSON.parse(jsonFile);
var SERV_HOST = jsonString['hostIp']
, SERV_PORT = jsonString['hostPort']
, CACHE_EXP = jsonString['cacheExp']
, CPS = jsonString['cps']
, xPowerBy = ':P'
, xFrameOptions = ''
, xXSSProtection = ''
, xContentTypeOption = ''
, cacheControl = '';
} catch (err) {
console.log('There is an error parsing the json file : ' + err);
}
// Load required modules
var fs = require('fs')
, web = require('node-static');
var public_dir = new web.Server(environment + '/_public');
var options = {
key: fs.readFileSync(environment + '/keys/key.pem')
, cert: fs.readFileSync(environment + '/keys/cert.pem')
// SPDY-specific options
, spdy: {
protocols: [ 'h2','spdy/3.1', 'spdy/3', 'spdy/2','http/1.1', 'http/1.0' ],
plain: false,
connection: {
windowSize: 1024 * 1024, // Server's window size
// **optional** if true - server will send 3.1 frames on 3.0 *plain* spdy
autoSpdy31: false
}
}
, host: SERV_HOST
, port: SERV_PORT
, cache: CACHE_EXP
};
// Load security setings via config.json
var security = [
{ name: 'X-Powered-By',
option: xPowerBy }
, { name: 'x-frame-options',
option: xFrameOptions }
, { name: 'X-XSS-Protection',
option: xXSSProtection }
, { name: 'X-Content-Type-Options',
option: xContentTypeOption }
, { name: 'Cache-Control',
option: CACHE_EXP }
, { name: 'Content-Security-Policy',
option: CPS }
, { name: 'server',
option: 'Who knows' }
];
if (process.env.NODE_ENV == 'production') {
require("spdy").createServer(options, function(req, res) {
// Add Content Security Rules
for(var i = 0; i < security.length; i++){
res.setHeader(security[i].name, security[i].option);
}
public_dir.serve(req, res, function (err, result) {
if (err) { // There was an error serving the file
console.error("Error serving " + req.url + " - " + err.message);
// Respond to the client
res.writeHead(err.status, err.headers);
res.end();
}
});
}).listen(options.port, options.host);
console.log("serving at https://" + options.host + ":" + options.port);
console.log("On Node v" + process.versions.node);
}
开箱即用。