Nodejs, call interface to modify HTTPS

Source: Internet
Author: User

1"Use Strict";2 3 varApp = require (' Express ')();4 varFS = require (' FS ');5 varHTTP = require (' http ');6 varHTTPS = require (' HTTPS ');7 varRequest = require (' request '));8 varBodyparser = require (' Body-parser '));9 varPrivatekey = Fs.readfilesync ('./private.pem ', ' UTF8 ');Ten varCertificate = Fs.readfilesync ('./file.crt ', ' UTF8 '); One varCakey = Fs.readfilesync ('./csr.pem ', ' UTF8 '); A varCredentials ={key:privatekey, Ca:[cakey], cert:certificate}; -  - varHttpserver =Http.createserver (app); the varHttpsserver =Https.createserver (credentials, app); - varPORT = 1110; - varSslport = 1111; - App.use (Bodyparser.json ()); +App.use (bodyparser.urlencoded ({extended:true})); -  +Httpserver.listen (PORT,function() { AConsole.log (' http://localhost:%s ', PORT); at }); -Httpsserver.listen (Sslport,function() { -Console.log (' https://localhost:%s ', sslport); - }); -  - functionhttpspostresult (URL, json, res) { in request.post (URL, {Json:json}, -         function(_error, response, body) { to             if(!_error && Response.statuscode = = 200) { + Res.json (body); -}Else { theConsole.log (' ERROR ')); *             } $         }Panax Notoginseng     ); - } the  +App.get ('/',function(req, res) { A     if(Req.protocol = = = ' HTTPS ') { theRes.status. Send (' https ')); +}Else { -Res.status. Send (' http ')); $     } $ }); - varHost = ' '; - /** the * SMS Verification Code -  */WuyiApp.post ('/test ',function(req, res, next) { the     varurl = host + '/test '; -     varJSON = { Wu Mobile:req.body.mobile -     }; About httpspostresult (URL, json, res); $});

Three files were generated: Private.pem: Private key, Csr.pem:CSR certificate signature, FILE.CRT: certificate file.

Created: Package.json

1 {2   "name": "Application-name",3   "version": "0.0.1",4    "Dependencies": {5     "Body-parser": "Latest",6     " Express ":" Latest ",7     " Request ":" Latest "8  }9 }

NPM Install Node_modules

Node Start service

Nodejs, call interface to modify HTTPS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.