首页 > 解决方案 > 简单的 https express 导致“此页面无法正常工作”

问题描述

我有一个非常简单的节点设置,只安装了 express。我将此命令从这里粘贴到我的 Ubuntu 20 bash中

openssl req -nodes -new -x509 -keyout server.key -out server.cert

当我运行此代码时:

const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const httpPort = 3000;
const httpsPort = 3001;
app = express();

var key = fs.readFileSync(__dirname + '/server.key');
var cert = fs.readFileSync(__dirname + '/server.cert');

var credentials = {
  key: key,
  cert: cert,
};

//GET home route
app.get('/', (req, res) => {
  res.send('Hello World.');
});

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(httpPort, () => {
  console.log('Http server listing on port : ' + httpPort);
});

httpsServer.listen(httpsPort, () => {
  console.log('Https server listing on port : ' + httpsPort);
});

当我访问 localhost:3001 时,这会导致错误

在此处输入图像描述

我究竟做错了什么?

在此先感谢您的时间!

标签: node.jsexpress

解决方案


我的错误是我localhost:3001在浏览器搜索中输入了请求http://localhost:3001

打字https://localhost:3001工作。


推荐阅读