温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用openSSL构造一个支持https的nodejs服务器

发布时间:2021-11-06 15:21:30 来源:亿速云 阅读:173 作者:iii 栏目:web开发

本篇内容主要讲解“怎么使用openSSL构造一个支持https的nodejs服务器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用openSSL构造一个支持https的nodejs服务器”吧!

首先通过下面的链接下载openSSL
https://slproweb.com/products/Win32OpenSSL.html

怎么使用openSSL构造一个支持https的nodejs服务器

怎么使用openSSL构造一个支持https的nodejs服务器

下载完毕后,执行openssl进入交互式界面:

怎么使用openSSL构造一个支持https的nodejs服务器

使用命令生成privatekey.pem 1024意思是1024位长度。

openssl genrsa -out privatekey.pem 1024

怎么使用openSSL构造一个支持https的nodejs服务器

生成的privatekey.pem,打开看一看长啥样:

怎么使用openSSL构造一个支持https的nodejs服务器

怎么使用openSSL构造一个支持https的nodejs服务器

什么是pem文件?

.pem - Defined in RFCs 1421 through 1424, this is a container format that may include just the public certificate (such as with Apache installs, and CA certificate files /etc/ssl/certs), or may include an entire certificate chain including public key, private key, and root certificates. Confusingly, it may also encode a CSR (e.g. as used here) as the PKCS10 format can be translated into PEM. The name is from Privacy Enhanced Mail (PEM), a failed method for secure email but the container format it used lives on, and is a base64 translation of the x509 ASN.1 keys.

简单的说,就是一个密钥文件。

第二步,基于第一步生成的密钥文件生成一个证书请求:
openssl req -new -key privatekey.pem -out certrequest.csr

怎么使用openSSL构造一个支持https的nodejs服务器

如果懒得维护证书明细,直接敲回车,会自动填入默认值:

怎么使用openSSL构造一个支持https的nodejs服务器

怎么使用openSSL构造一个支持https的nodejs服务器

怎么使用openSSL构造一个支持https的nodejs服务器

最后基于第一步生成的密钥和证书请求生成一个数字证书:当然颁发机构就是自己了,仅用于测试目的。
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

怎么使用openSSL构造一个支持https的nodejs服务器

怎么使用openSSL构造一个支持https的nodejs服务器

至此我们有了privatekey.pem和Certificate.pem两个证书了。

怎么使用openSSL构造一个支持https的nodejs服务器

下面是我https服务器的代码,很简单,只有50几行:

var app = require('express')();var fs    = require('fs');var https = require('https');var httpOptions =  { key: fs.readFileSync("keys/privatekey.pem"), cert: fs.readFileSync("keys/certificate.pem")
}var server = https.createServer(httpOptions, app);var io = require('socket.io')(server);console.log("https server listens on port 8080...");
server.listen(8080);function print_env(){  console.log(process.env);
}
app.get('/', function (req, res) {  var response = "Hello World";
  res.send(response);
});
app.get('/env', function (req, res) {
  print_env();  // res.sendFile(__dirname + '/index.html');
  var response = JSON.stringify(process.env);
  res.send(response);
});
app.get('/redis', function (req, res) {  var redisClient = require("./redisClient");  
  function callback(response){    // var response = "ok";//JSON.stringify(process.env);
    res.send(response);
  }
  redisClient.test(callback);
});
io.on('connection', function (socket) {  console.log("connect comming from client: " + socket.id);
  socket.emit('messages_jerry', { hello: 'world greeting from Server!' });
  socket.on('messages', function (data) {    console.log("data received from Client:" + JSON.stringify(data,2,2));
  });
});

从代码里不难理解这两个pem文件是如何用在https服务器里的。
最后在浏览器里测试。因为是自己颁发的证书,没有经过CA验证,所以浏览器会显示一个警告。

怎么使用openSSL构造一个支持https的nodejs服务器

到此,相信大家对“怎么使用openSSL构造一个支持https的nodejs服务器”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI