使用openSSL构造一个支持https的nodejs服务器
首先通过下面的链接下载openSSL
https://slproweb.com/products/Win32OpenSSL.html


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

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

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


什么是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 x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem


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

下面是我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验证,所以浏览器会显示一个警告。

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

使用openSSL构造一个支持https的nodejs服务器的更多相关文章
- 用Docker搭建一个支持https的nginx代理服务
用Docker搭建一个支持https的nginx代理服务 说明:本文所提的服务只是作者平常测试使用,可能含有未知bug或不成熟的解决方案,仅供参考,请不要用于正式环境,当然,使用过程中有任何问题欢迎提 ...
- 创建一个支持ES6的Nodejs项目
文章来自于:https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o 第一步:创建项目文件夹并初始化 ...
- node如何让一个端口同时支持https与http
众所周知node是一个高性能的web服务器,使用它可以很简单的创建一个http或https的服务器. 比如一个很简单的http服务器: var http = require('http'); var ...
- Linux下设置Apache支持Https服务
HTTPS的主要作用: 1)建立一个信息安全通道,来保证数据传输的安全性 2)确认网站的真实性 HTTPS与HTTP的区别: 1)HTTPS协议需要到ca申请证书,免费证书较少 2)HTTP是超文本传 ...
- 实现KbmMw web server 支持https
在以前的文章里面介绍过kbmmw 做web server. 前几天红鱼儿非要我给他做一个支持https 的web server. 其实kbmmw 支持https 有好几种方法: 1. 使用isapi ...
- 在window平台下,自己DIY编译OpenSSL,Libcurl ,来支持HTTPS传输协议
1 缘起 原来就了解些libcurl,一直没有机会在项目实际使用libcurl. 恰好最近一个云存储的项目,服务器使用openstack 恰好我负责现在的一个云存储SDK c++版本的开发中. 与 ...
- Ubuntu+NDK编译openssl(为了Android上使用libcurl且支持HTTPS协议)
为了Android上使用libcurl且支持HTTPS协议,需要依赖openssl,因此先来了解一下如何编译OpenSSL1.编译ARM下的共享库(默认的)我使用的是guardianproject的o ...
- nginx 监听一个端口同时支持https和http
nginx 如何想同时支持https和http,必须监听两个不同的端口,比如http:listen 80; https:listen 443; server { listen 1234 ssl;s ...
- 如何让你的网站支持https
如何让你的网站支持https 当今世界的主流网站基本都是使用https对外界提供服务,甚至有某些公司建议完全使用https, 那么https是什么呢?请参考如下的图解,https是在我们通常说的tcp ...
随机推荐
- python select模块
Python select 一.前言 Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句 ...
- linux简单命令7--管道符和通配符
”&&“和管道符“|”不一样. ---------------------------------------------------------通配符---------------- ...
- 小记LoadRunner 11 安装VC2005运行环境报错处理
这几天在做性能优化,需要在虚拟机里装个LoadRunner 11.从测试同学那里搞来安装包,按照文档提示安装系统运行环境,提示我要装VC2005 SP1. 安装程序自己安装,报错.截图如下. 于是我又 ...
- appium1.4.1版本下载
链接:https://pan.baidu.com/s/1PvgeoPNW6bJg50uguL9fpA 密码:0fm7
- unity 读取灰度图生成按高程分层设色地形模型
准备灰度图 1.高程按比例对应hue色相(hsv)生成mesh效果 o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0 ...
- mac Access denied for user 'root'@'localhost' (using password: YES)
1:苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务 2: Start it in safe mode 进入终端 输入: cd /usr/local/mysql ...
- 【VS开发】四大图像库:OpenCV/FreeImage/CImg/CxImage
本文转载自:http://hi.baidu.com/xiaocuiman/blog/item/6e267c2bc4b1883f5243c108.html 1.对OpenCV 的印象:功能十分的强大,而 ...
- Linux系统下GDB调试
GDB 一.gdb常用命令: 命令 描述 backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 in ...
- Reactor系列(三)创建Flux,Mono(续)
创建Mono 视频讲解:https://www.bilibili.com/video/av78944069/ FluxMonoTestCase.java package com.example.rea ...
- UWP笔记-使用FFmpeg编解码
在开发UWP媒体应用的时候,使用的MediaElement可以支持主流的格式,不过还是有些格式本地编解码器是不支持的,如.flv..rmvb等,这里讲到的是第三方开源库FFmpeg,可以直接播放更多的 ...