[英](Node.js) Trouble with SSL Express Server
我有以下代碼:
const http = require('http');
const https = require('https');
const server = http.Server(app); // ssl commented
const socket = require('socket.io')(server); // ssl commented
const WebSocket = require('ws');
const fs = require('fs');
const PORT = 80;
// we will pass our 'app' to 'https' server
// const server = https.createServer({
// key: fs.readFileSync(__dirname+'/configuration/key.pem'),
// cert: fs.readFileSync(__dirname+'/configuration/cert.pem'),
// passphrase: '<you wish!>'
// }, app)
//
// server.listen(PORT, function(){
// console.log(`Listening on http://localhost:${PORT}`);
// //vncClient = new WebSocket.Server({server: server});
// vncClient = new WebSocket.Server({port: 3000});
// vncClient.on('connection', new_client);
// });
// const socket = require('socket.io')(server);
server.listen(PORT, function(){ // ssl commented
console.log(`Listening on http://localhost:${PORT}`); // ssl commented
vncClient = new WebSocket.Server({port: 3000}); // ssl commented
vncClient.on('connection', new_client); // ssl commented
}); // ssl commented
當我切換注釋時(即實際注釋行未注釋,而“ssl 注釋”行實際上已注釋),我得到正常的控制台輸出(無錯誤),但瀏覽器中沒有任何渲染。 為什么我可以生成http
服務器但不能生成https
? 我用OpenSSL
生成了.pem
的
我能夠使用節點https
模塊運行 HTTPS 服務器。 使用以下步驟生成自簽名證書:
openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout example.key -out example.crt
確保自簽名您有正確的 CN 名稱。 就我而言,它是本地主機
這是示例代碼:
var app = require('express')();
const http = require('http');
const https = require('https');
var path = require('path');
const WebSocket = require('ws');
const fs = require('fs');
const PORT = 8080;
const server = https.createServer({
key: fs.readFileSync(path.resolve(__dirname+'/example.key')),
cert: fs.readFileSync(path.resolve(__dirname+'/example.crt')),
rejectUnauthorized: false
}, app)
server.listen(PORT, function(){
console.log(`Listening on https://localhost:${PORT}`);
//vncClient = new WebSocket.Server({server: server});
// vncClient = new WebSocket.Server({port: 3000});
// vncClient.on('connection', new_client);
});
var io = require('socket.io').listen(server);
io.sockets.on('connection',function (socket) {
console.log("listening..")
});
app.get("/", function(request, response){
response.send("Hello World").status(200)
});
// server.listen(PORT, function(){ // ssl commented
// console.log(`Listening on http://localhost:${PORT}`); // ssl commented
// vncClient = new WebSocket.Server({port: 3000}); // ssl commented
// vncClient.on('connection', new_client); // ssl commented
// });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.