用Node.js实现基于https的Restful风格webservice

1. 创建证书文件

bash-3.2$ 

bash-3.2$ mkdir ssl

bash-3.2$ cd ssl

bash-3.2$ openssl genrsa -out key.pem 1024

Generating RSA private key, 1024 bit long modulus

....................................++++++

..........++++++

e is 65537 (0x10001)

bash-3.2$ openssl req -new -key key.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]:

State or Province Name (full name) [Some-State]:

Locality Name (eg, city) []:

Organization Name (eg, company) [Internet Widgits Pty Ltd]:

Organizational Unit Name (eg, section) []:

Common Name (e.g. server FQDN or YOUR name) []:

Email Address []:


Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

bash-3.2$ 

bash-3.2$ 

bash-3.2$ openssl x509 -req -in certrequest.csr -signkey key.pem -out cert.pem

Signature ok

subject=/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd

Getting Private key

bash-3.2$ 

bash-3.2$ 


2. vim app.js

  1 var express = require('express'), https = require('https'), fs = require('fs');

  2 

  3 var privateKey = fs.readFileSync('./ssl/key.pem').toString();

  4 var certificate = fs.readFileSync('./ssl/cert.pem').toString();

  5 var ca = fs.readFileSync('./ssl/certrequest.csr').toString();

  6 

  7 var options = {

  8       key : privateKey,

  9       cert : certificate,

 10       ca : ca

 11 }

 12 

 13 var app = express();

 14 

 15 //RESTful API

 16 app.get("/testapi"function(req, res){

 17     res.send('test api');

 18 });

 19 

 20 app.get("/"function(req, res){

 21   res.send('');

 22 });

 23 

 24 https.createServer(options, app).listen(443, function() {

 25      console.log('https server started successfully.');

 26 });

 27 

 28 app.listen(80);



3. node app


4.用浏览器打开:https://127.0.0.1/testapi










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/1701038,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值