javascript代码实例教程-用Node.js实现基于https的Restful风格webservice

发布时间:2019-01-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-用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

 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-用Node.js实现基于https的Restful风格webservice全部内容,希望文章能够帮你解决javascript代码实例教程-用Node.js实现基于https的Restful风格webservice所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。