尝试 运行 将节点 js 表示为 https 服务器,但不会 运行

Trying to run express node js as https server but it won't run

我正在尝试让 HTTPS 在节点的 express.js 上运行,但它不会 运行。

这是我的 server.js 代码。

const fs = require('fs');
const http = require ('http');
const https = require('https');

const options = {
    pfx: fs.readFileSync('ssl/pfxfile.pfx'),
    passphrase: 'password'
};
const express = require('express');
const app = express();

const path = require('path');
    app.use(express.json());
    app.use(express.static("express"));
    app.use('/', function(req,res){
        res.sendFile(path.join(__dirname+'/express/index.html'));
    });
 
var httpServer = http.createServer(app);
var httpsServer = https.createServer(options, app);

httpServer.listen(8080);
httpsServer.listen(8443);

当我 运行 时,它没有报告任何错误,但它只是卡住了(我等了 30 分钟,看看它是否做了什么,但什么也没发生)。

httpServer.listen(8080, ()=>{console.log('Server is running')}); 如果服务器成功启动,它应该在控制台输出“Server is 运行”。这是检查服务器是否按预期工作的好方法。

我发现了我的错误,感谢您的回答,它一直在帮助我,我的错误首先是我没有输入任何 console.log,其次是我没有在浏览器中输入 8443。

const fs = require('fs');
const http = require('http');
const https = require('https');

const options = {
    pfx: fs.readFileSync('ssl/pfxfile.pfx'),
    passphrase: 'password'
};
const express = require('express');
const app = express();

const path = require('path');
    app.use(express.json());
    app.use(express.static("express"));
    app.use('/', function(req,res){
        res.sendFile(path.join(__dirname+'/express/index.html'));
    });
 
const httpServer = http.createServer(app);
const port = process.env.PORT || 8080;
const httpsServer = https.createServer(options, app);
const portHttps = process.env.PORT || 8443;

httpServer.listen(port, () => console.log('Http listening on port ' + port));
httpsServer.listen(portHttps, () => console.log('Https listening on port ' + portHttps));