有没有办法在Express上为SSL设置自定义端口?这仅用于开发目的。我在80/443上有一个Apache / PHP项目。
我尝试过这个教程 http://www.hacksparrow.com/express-js-https.html
但说实话,我无法让它发挥作用。如果有人可以发布配置Express的正确方法来执行此操作,那将是非常棒的。这是我的服务器配置:
var hskey = fs.readFileSync('mywebsite-key.pem');
var hscert = fs.readFileSync('mywebsite-cert.pem')
var options = {
key: hskey,
cert: hscert
};
//Create server
var app = express(options);
// Configure server
app.configure(function () {
//parses request body and populates request.body
app.use(express.bodyParser());
//checks request.body for HTTP method overrides
app.use(express.methodOverride());
//perform route lookup based on url and HTTP method
app.use(app.router);
//Where to serve static content
app.use(express.static(path.join(application_root, 'public')));
//Show all errors in development
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
//Start server
var port = nconf.get('port');;
var server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(port, function () {
console.log('Express server listening on port %d in %s mode', port, app.settings.env);
});
答案 0 :(得分:5)
您正在初始化服务器错误。首先,您使用require('http')
作为服务器,因此它不会使用SSL选项。其次,您将证书选项传递给express,而不是https服务器。
你必须这样做:
var hskey = fs.readFileSync('mywebsite-key.pem');
var hscert = fs.readFileSync('mywebsite-cert.pem')
var options = {
key: hskey,
cert: hscert
};
var express = require('express'),
app = express()
, https = require('https')
, server = https.createServer(options,app)
, io = require('socket.io').listen(server);
// listen for new web clients:
server.listen(8080);