gpt4 book ai didi

node.js - 在自定义端口上设置 Express/Node SSL

转载 作者:搜寻专家 更新时间:2023-11-01 00:25:55 25 4
gpt4 key购买 nike

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

最佳答案

您错误地初始化了服务器。首先,您正在为服务器使用 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);

关于node.js - 在自定义端口上设置 Express/Node SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17666013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com