广告
返回顶部
首页 > 资讯 > 服务器 >怎么使用openSSL构造一个支持https的nodejs服务器
  • 552
分享到

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

2024-04-02 19:04:59 552人浏览 八月长安
摘要

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

本篇内容主要讲解“怎么使用openSSL构造一个支持httpsnodejs服务器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用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服务器”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: 怎么使用openSSL构造一个支持https的nodejs服务器

本文链接: https://www.lsjlt.com/news/85532.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么使用openSSL构造一个支持https的nodejs服务器
    本篇内容主要讲解“怎么使用openSSL构造一个支持https的nodejs服务器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用openSSL构造一个支...
    99+
    2022-10-19
  • 使用Java怎么建立一个稳定的多线程服务器
    本篇文章为大家展示了使用Java怎么建立一个稳定的多线程服务器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。消息系统的建立这套服务器的消息系统采用的是对象传输的机制...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作