Nodejs创建https服务器(Windows 7)
为了实验一下WebRTC,搭了个简单的https服务器。说说步骤:
- 生成OpenSSL证书
- 使用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)的更多相关文章
- Nodejs创建HTTPS服务器
Nodejs创建HTTPS服务器 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Jav ...
- Nodejs+Express创建HTTPS服务器
为了使我的Nodejs服务器提供HTTPS服务,学习了一下如何利用express创建https服务器,现记录如下.(一点一点的积累与掌握吧) 1. Http与Https 介绍 HTTP: 超文本传输协 ...
- nodejs的express框架创建https服务器
一 openssl创建https私钥和证书 1.下载windows版openssl: http://slproweb.com/products/Win32OpenSSL.html Win64OpenS ...
- nodejs创建http服务器
之前有简单介绍nodejs的一篇文章(http://www.cnblogs.com/fangsmile/p/6226044.html) HTTP服务器 Node内建有一个模块,利用它可以很容易创建基本 ...
- NodeJS搭建HTTPS服务器
[NodeJS搭建HTTPS服务器] http://cnodejs.org/topic/54745ac22804a0997d38b32d
- NodeJs 创建 Web 服务器
以下是演示一个最基本的 HTTP 服务器架构(使用8081端口),创建 ser.js 文件,代码如下所示: var http = require('http'); var fs = require(' ...
- 【本地服务器】json-server搭建本地https服务器(windows)
(一)用json-server搭建简单的服务器 (搭建出来的服务器地址为localhost:3000) 1.新建Mockjson文件夹,进入该文件夹目录,运行命令 npm install -g jso ...
- 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 发送服务器 ...
- Express与NodeJs创建服务器的两种方法
NodeJs创建Web服务器 var http = require('http'); var server = http.createServer(function(req, res) { res.w ...
随机推荐
- spring3表达式语言(SpEL)
使用SpEl进行表达式操作,基本操作如下: <!--第一步,构建解析 --> ExpressionParser parser = new SpelExpressionParser(); & ...
- java二维数组简单初步理解
二维数组 二维数组本质上是以数组作为数组元素的数组,即“数组的数组”. int[][] arr = {{1, 2, 3}, {4, 5, 6}}; System.out.println(arr[0][ ...
- C#窗体计算器
使用窗体写的计算器小程序,不是十分完善,键盘输入只能输入数字键,其他需要换档键的键的输入没有搞懂,先发上来左右,以作留存. 界面截图 主要使用的是TextBox和button控件 using Syst ...
- 简单管理用户SESSION小记(个人观点,欢迎斧正)
做了几年码农,记录下一般涉及到用户session管理的方法. 问题说明: a.用户如果点击退出时,可以获取用户动作,这样可以销毁session. b.用户直接关闭浏览器或者直接意外关机情况,无法获取用 ...
- Storm(4) - Distributed Remote Procedure Calls
Using DRPC to complete the required processing 1. Create a new branch of your source using the follo ...
- intel vt-x处于禁用状态下如何处理
1.首先看你的bios选项里面有没有该选项,如果没有就更新,更新之后还没有,则不支持 2.找到intel Virtualization Technology 将状态改为Enabled 同时找到int ...
- 如何管理好项目的DLL
.net fx自带的dll net fx自带的dll,直接添加,注意.net fx版本即可. 第三方类库 如果是第三方类库,使用NuGet从NuGet官网下载,比如json.net,jQuery等. ...
- How to Run a .Jar Java File
.jar files are used for archiving, archive unpacking. One of the essential features of jar file is l ...
- php pthreads 多线程扩展的使用:一个较为稳定例子。
今天研究了worker stackable的配合方法,写了两种形式,虽然能工作,但是都会出现内存不听增长的问题: 于是把第一个方法的代码邮件给了作者,到现在他没有回复我. 我最后放弃两者配合的方式,直 ...
- CodeForces 546A-Soldier and Bananas
题意: 有n dollar,the first banana cost k dollars,第i个就需cost k*i,问买w个bananas是否需要借钱:借钱需要多少? 分析:首先计算w个bana ...