代码之家  ›  专栏  ›  技术社区  ›  Sterling Butters

(Node.js)SSL Express服务器故障

  •  0
  • Sterling Butters  · 技术社区  · 6 年前

    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 ? 我生成了 .pem OpenSSL

    0 回复  |  直到 6 年前
        1
  •  0
  •   Sohan    6 年前

    我可以使用node运行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
    // });