为了实验一下WebRTC,搭了个简单的https服务器。说说步骤:

  1. 生成OpenSSL证书
  2. 使用Nodejs的https模块建立服务器

OpenSSL 证书

我机子Windows 7,安装了Cygwin,里面安装了openssl,我用它来生成https服务器需要的证书文件。

1. 生成私钥key文件

$ openssl genrsa -out privatekey.pem 1024

Generating RSA private key, 1024 bit long modulus
....++++++
.....................................++++++
e is 65537 (0x10001)

2. 通过私钥生成CSR证书签名

$ openssl req -new -key privatekey.pem -out certrequest.csr

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:ShanXi
Locality Name (eg, city) []:Xian
Organization Name (eg, company) [Internet Widgits Pty Ltd]:foruok
Organizational Unit Name (eg, section) []:foruok
Common Name (e.g. server FQDN or YOUR name) []:foruok
Email Address []:foruok@163.com Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

3. 通过私钥和证书签名生成证书文件

$ openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certification.pem

Signature ok
subject=/C=CN/ST=ShanXi/L=Xian/O=foruok/OU=foruok/CN=foruok/emailAddress=foruok@163.com
Getting Private key

Nodejs https 服务器示例

从Nodejs文档里摘出来的代码:


const https = require('https');
const fs = require('fs'); const options = {
key: fs.readFileSync('./privatekey.pem'),
cert: fs.readFileSync('./certification.pem')
}; https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);

执行 node httpsdemo.js,然后在Chrome里访问access https://192.168.40.158:8000。结果如下:

你可能注意到了,证书有点问题,点下地址栏,能看怎么回事儿:

我们创建的证书没有经过身份验证,不受信。有需要的话,可以去有资质的网络运营商申请自己的证书。

Nodejs创建https服务器(Windows 7)的更多相关文章

  1. Nodejs创建HTTPS服务器

    Nodejs创建HTTPS服务器 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Jav ...

  2. Nodejs+Express创建HTTPS服务器

    为了使我的Nodejs服务器提供HTTPS服务,学习了一下如何利用express创建https服务器,现记录如下.(一点一点的积累与掌握吧) 1. Http与Https 介绍 HTTP: 超文本传输协 ...

  3. nodejs的express框架创建https服务器

    一 openssl创建https私钥和证书 1.下载windows版openssl: http://slproweb.com/products/Win32OpenSSL.html Win64OpenS ...

  4. nodejs创建http服务器

    之前有简单介绍nodejs的一篇文章(http://www.cnblogs.com/fangsmile/p/6226044.html) HTTP服务器 Node内建有一个模块,利用它可以很容易创建基本 ...

  5. NodeJS搭建HTTPS服务器

    [NodeJS搭建HTTPS服务器] http://cnodejs.org/topic/54745ac22804a0997d38b32d

  6. NodeJs 创建 Web 服务器

    以下是演示一个最基本的 HTTP 服务器架构(使用8081端口),创建 ser.js 文件,代码如下所示: var http = require('http'); var fs = require(' ...

  7. 【本地服务器】json-server搭建本地https服务器(windows)

    (一)用json-server搭建简单的服务器 (搭建出来的服务器地址为localhost:3000) 1.新建Mockjson文件夹,进入该文件夹目录,运行命令 npm install -g jso ...

  8. Node.js权威指南 (8) - 创建HTTP与HTTPS服务器及客户端

    8.1 HTTP服务器 / 177 8.1.1 创建HTTP服务器 / 177 8.1.2 获取客户端请求信息 / 182 8.1.3 转换URL字符串与查询字符串 / 184 8.1.4 发送服务器 ...

  9. Express与NodeJs创建服务器的两种方法

    NodeJs创建Web服务器 var http = require('http'); var server = http.createServer(function(req, res) { res.w ...

随机推荐

  1. hdu---(5038)Grade(胡搞)

    Grade Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Sub ...

  2. Login 页面

    1.jsp <script type="text/javascript"> function doLogin() { if (trim($('#username').v ...

  3. 让ie678支持css一些属性及html标签

    昨天写的一个页面,用的css3及html5的一些样式与标签,在ie8下看是没有效果的,然后就在晚上查找了一下如何能让ie8也能实现这些效果. 1.添加respond.js文件,Respond.js让I ...

  4. netty4 Handler的执行顺序

    转载:https://my.oschina.net/jamaly/blog/272385 Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通 ...

  5. 关于php用simplexml_load_string解析xml出现乱码的小结

    最近在做项目时需要通过xml接口读取合作伙伴数据到数据库,在xml解析环节出现有些特殊中文字符乱码的现象.后采取下面的办法终于解决. 1.curl 抓取过来的字符是unicode编码,需要先转换为ut ...

  6. mouseleave 与 mouseout 的不同

    Q:给某div添加mouseout事件后,在空白区域移动到其子元素(如按钮)上(此时并没有离开此div)时,会触发mouseout事件,而mouseleave则不会 A:与 mouseout 事件不同 ...

  7. HDU 3265 扫描线(矩形面积并变形)

    Posters Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. UVa 11427 - Expect the Expected

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. C# 子窗体点击按钮产生的新子窗体放在父窗体里

    情景展示: 父窗体Form1,左边是按钮,右边是panel(放置子窗体) 父窗体点击按钮,在panel显示第一个子窗体AA, AA有个按钮,点击按钮,是第二个子窗体ZZ, 怎样将AA的子窗体ZZ也显示 ...

  10. IT公司100题-7-判断两个链表是否相交

    问题:有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环.1.如何判断一个链表是不是这类链表? 问题扩展:1.如果链表可能有环呢?2.如果需 ...