用Node.js實現基於https的Restful風格webservice

來源:互聯網
上載者:User

用Node.js實現基於https的Restful風格webservice
1. 建立認證檔案bash-3.2$ bash-3.2$ mkdir sslbash-3.2$ cd sslbash-3.2$ openssl genrsa -out key.pem 1024Generating RSA private key, 1024 bit long modulus....................................++++++..........++++++e is 65537 (0x10001)bash-3.2$ openssl req -new -key key.pem -out certrequest.csrYou are about to be asked to enter information that will be incorporatedinto 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 blankFor 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' attributesto be sent with your certificate requestA 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.pemSignature oksubject=/C=AU/ST=Some-State/O=Internet Widgits Pty LtdGetting Private keybash-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 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.