为了实验一下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. 更准确的mysql全文索引

    MYSQL自带的全文索引在查找数据的时候,有非常多的限制,如字符少于3个不能搜索,常用字不能搜索 但mysql 的like进行查询的时候又非常的慢 但你需要用到比较准确的查询的时候,要么不用mysql ...

  2. 简单springMVC环境搭建

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  3. javamail 学习及实例

    JavaMail,是Sun发布的用来处理email的API,提供给开发者处理电子邮件相关的编程接口.通过使用它可以方便地执行一些常用的邮件传输. 可以看下<java邮件开发详解>第四章和第 ...

  4. JButton按钮

    1.方法 void  setSize(width,height):设置按钮大小 void  setBounds(x,y,width,heigth):设置按钮的左上角顶点位置和大小 void  setC ...

  5. Java集合——Set接口

    1.定义 set中不允许放入重复的元素(元素相同时只取一个),使用equals()方法进行比较.如果返回true,两个对象的HashCode值也应该相等 2.方法 add():添加元素 remove( ...

  6. 使用Yeoman,Grunt和Bower开发AngularJS(译)

    使用Yeoman产生AngularJS的主要骨架 使用Grunt加速开发和帮助执行 使用Bower来加入第三方插件和框架——third party plugins/frameworks 一.准备工作 ...

  7. shell 下的$符合

    $n        $1 the first parameter,$2 the second...$#        The number of command-line parameters.$0  ...

  8. Masonry使用案列详解

    案例一: 要求:无论在什么尺寸的设备上(包括横竖屏切换),红色view都居中显示.

  9. eclipse常用10个快捷键[转载]

    转载自:http://www.jb51.net/softjc/139467.html

  10. call,apply,bind函数

    一.call函数 a.call(b); 简单的理解:把a对象的方法应用到b对象上(a里如果有this,会指向b) call()的用法:用在函数上面 var Dog=function(){ this.n ...