3

有没有办法在 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);
});
4

1 回答 1

5

您正在初始化服务器错误。首先,您使用require('http')的是服务器,因此它不会使用 SSL 选项。其次,您正在传递证书选项来表达,而不是 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);
于 2013-07-16T04:34:09.173 回答