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