我正在尝试使用molnarg的node-http2模块在节点服务器上实现HTTP / 2。我原来的server.js运行正常,当我实现HTTP / 2模块时,我得到了一个
ERR_CONNECTION_REFUSED
来自浏览器(使用ChromeBook上的Chrome)。
实施HTTP / 2的更改列表:
hostPort
var更改为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,
});
}
答案 0 :(得分:0)
使用node-spdy的实现来代替node-http2,因为这个模块可以在node.js中创建具有自然http模块接口的HTTP2 / SPDY服务器,并且可以回退到常规的https(对于那些不喜欢的浏览器)。 t既不支持HTTP2,也不支持SPDY)。
above 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);
}
这开箱即用。