使用OpenSSL生成了自签名证书,并将证书和私钥复制到所需的目标文件夹中。

要创建HTTPS服务器,我们需要做两件事:SSL证书和Node的内置https模块。

安装Node.js后,我尝试从命令行运行以下JavaScript
TLSServer.js

var tls = require('tls');
var fs = require('fs');
var port = 8081; //3000;
var host = '127.0.0.1'; //192.168.1.135
var options = {
        key: fs.readFileSync('private-key.pem'), // /path/to/private-key.pem
        cert: fs.readFileSync('certificate.pem') // /path/to/certificate.pem
    };



TLSClient.js

var client = tls.connect(port, host, options, function() {
    console.log('connected');
    if (client.authorized) {
        console.log('authorized: ' + client.authorized);
        client.on('data', function(data) {
            client.write(data);    // Just send data back to server
        });
    } else {
        console.log('connection not authorized: ' + client.authorizationError);
    }
});


实际输出:

cmd>node TLSServer.js
openssl config failed: error:02001005:system library:fopen:Input/output error




cmd>node TLSClient.js
openssl config failed: error:02001005:system library:fopen:Input/output error
events.js:193
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT 127.0.0.1:8081
    at Object._errnoException (util.js:1031:13)
    at _exceptionWithHostPort (util.js:1052:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)


导致此问题的原因可能是什么:
openssl配置失败:错误:02001005:系统库:fopen:输入/输出错误

httpserver.js

var fs = require('fs');
var https = require('https');

var options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('certificate.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.write("You are connected to https server");
  res.end("\n hello world \n");
}).listen(8080)



  https://localhost:8080


从浏览器中,我曾经获得以下输出:

You are connected to https server
  hello world


但不适用于TLS客户端/服务器。但是在OpenSSL配置文件中可能要修改什么?

最佳答案

通过在环境变量->系统变量中添加openssl.cnf的路径来解决openssl config failed: error:02001005:system library:fopen:Input/output error

OPENSSL_CONF=C:\OpenSSL-Win64\bin\openssl.cnf


要验证它,您可以在外壳中输入:

echo %OPENSSL_CONF%


但是我仍然在使用TLSServer.js时出错

cmd>node TLSServer.js




 module.js:544
        throw err;
        ^

    Error: Cannot find module 'C:\Users\user\Desktop\TLSServer.js'
        at Function.Module._resolveFilename (module.js:542:15)
        at Function.Module._load (module.js:472:25)
        at Function.Module.runMain (module.js:682:10)
        at startup (bootstrap_node.js:191:16)
        at bootstrap_node.js:613:3

09-10 00:01
查看更多