node.js - 简单的 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 时,这会导致错误
我究竟做错了什么?
在此先感谢您的时间!
解决方案
我的错误是我localhost:3001
在浏览器搜索中输入了请求http://localhost:3001
。
打字https://localhost:3001
工作。
推荐阅读
- ffmpeg - 使用 libav* 库实现 FFMpeg -r 和 -vf 之类的功能?
- c# - target=_blank 属性在文字字符串中不起作用
- php - Laravel/JWT token 什么时候被列入黑名单?
- java - 为什么我的 JavaFX 代码会出现 InvocationTargetException?
- java - 如何使用改造处理 JSON 对象中的 NULL 值
- xcode - Xcode Crash NO_CRASH_STACK + 0 没有堆栈跟踪和未符号化
- python - 如何生成用户所需的 JSON 文件
- json - Firebase 规则 - 如何将值限制为布尔值?
- javascript - HTML 中的 SVG:如何使用多色 SVG 作为遮罩
- postgresql - 迟来的分区可能吗?