资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用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

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

  2 

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

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

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

  6 

  7var options = {

  8      key : privateKey,

  9      cert : certificate,

 10      ca : ca

 11 }

 12 

 13var app = express();

 14 

 15//RESTful API

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

 17    res.send('test api');

 18 });

 19 

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

 21  res.send('');

 22 });

 23 

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

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

 26 });

 27 

 28app.listen(80);

3. node app

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


分享名称:用Node.js实现基于https的Restful风格webservice
标题路径:http://cdkjz.cn/article/pchdoi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220