Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Node.js中使用HTTPS_Node.js_Http_Ssl_Https_Openssl - Fatal编程技术网

如何在Node.js中使用HTTPS

如何在Node.js中使用HTTPS,node.js,http,ssl,https,openssl,Node.js,Http,Ssl,Https,Openssl,我对HTTPS、SSL等几乎没有经验 我想知道如何在HTTPS中使用Node.js。我知道如何使用node.js,但使用HTTPS时会出现错误 我想我需要安装一些东西(openSSL?)。为了使用node.js HTTPS服务器,我想知道我必须在windows 8.1计算机上安装的所有东西(不,我不想安装任何形式的linux。也不想安装cygwin) 我不需要有一个付费证书,我只需要有它的工作。它不接收来自浏览器的请求,因此我不关心付费证书。一旦在系统上安装了node.js,只需按照以下过程运行

我对HTTPS、SSL等几乎没有经验

我想知道如何在HTTPS中使用Node.js。我知道如何使用node.js,但使用HTTPS时会出现错误

我想我需要安装一些东西(openSSL?)。为了使用node.js HTTPS服务器,我想知道我必须在windows 8.1计算机上安装的所有东西(不,我不想安装任何形式的linux。也不想安装cygwin)


我不需要有一个付费证书,我只需要有它的工作。它不接收来自浏览器的请求,因此我不关心付费证书。

一旦在系统上安装了node.js,只需按照以下过程运行一个支持HTTP和HTTPS的基本web服务器

步骤1:构建证书颁发机构
  • 创建要存储密钥和证书的文件夹:

    mkdir conf


  • 转到该目录:

    cd-conf


  • 抓取此
    ca.cnf
    文件用作配置快捷方式:

    wgethttps://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/ca.cnf


  • 使用此配置创建新的证书颁发机构:

    openssl-req-new-x509-days 9999-config ca.cnf-keyout ca-key.pem-out ca-cert.pem


  • 现在我们在
    ca-key.pem
    ca-cert.pem
    中拥有了证书颁发机构,让我们为服务器生成一个私钥:

    openssl genrsa-out key.pem 4096


  • 抓取此
    server.cnf
    文件用作配置快捷方式:

    wgethttps://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/server.cnf


  • 使用以下配置生成证书签名请求:

    openssl-req-new-config server.cnf-key-key.pem-out csr.pem


  • 签署请求:

    openssl x509-req-extfile server.cnf-999天-passin“pass:password”-在csr.pem-CA-cert.pem-CAkey CA-key.pem-CAcreateserial-out cert.pem中


  • 步骤2:将证书安装为根证书
  • 将您的证书复制到根证书文件夹:

    sudo cp ca-crt.pem/usr/local/share/ca certificates/ca-crt.pem


  • 更新CA存储:

    sudo更新ca证书


  • 步骤3:启动节点服务器 首先,确保
    server.js
    的代码如下所示:

    var http = require('http');
    var https = require('https');
    var fs = require('fs');
    
    var httpsOptions = {
        key: fs.readFileSync('/path/to/HTTPS/server-key.pem'),
        cert: fs.readFileSync('/path/to/HTTPS/server-crt.pem')
    };
    
    var app = function (req, res) {
      res.writeHead(200);
      res.end("hello world\n");
    }
    
    http.createServer(app).listen(8888);
    https.createServer(httpsOptions, app).listen(4433);
    
  • 转到
    server.js
    所在的目录:

    cd/path/to


  • 运行
    server.js

    node server.js