Node.js开发 ---- http转https

来源:互联网 发布:长征是逃跑 知乎 编辑:程序博客网 时间:2024/06/05 08:02

从上周一折腾小程序

wx.request发起需要https请求

顺便给自己网站换一下证书


1.sslforfree注册申请证书(三个月有效期)

详细的步骤戳这里http://www.chinaz.com/web/2016/0216/504896.shtml


2.下载

密钥   private.key 

证书 certificate.crt


3.app.js

var app = require('express')();var fs = require('fs');var http = require('http');var https = require('https');var privateKey  = fs.readFileSync('./keys/private.key', 'utf8');var certificate = fs.readFileSync('./keys/certificate.crt', 'utf8');var credentials = {key: privateKey, cert: certificate};var httpServer = http.createServer(app);var httpsServer = https.createServer(credentials, app);var PORT = 18080;var SSLPORT = 443;httpServer.listen(PORT, function() {    console.log('HTTP Server is running on: http://localhost:%s', PORT);});httpsServer.listen(SSLPORT, function() {    console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);});// Welcomeapp.get('/', function(req, res) {    if(req.protocol === 'https') {        res.status(200).send('Welcome https!');    }    else {        res.status(200).send('Welcome http!');    }});



4.that is all






未经认证自创证书的教程戳

http://www.jianshu.com/p/853099ae2edd

http://blog.fens.me/nodejs-https-server/



0 0
原创粉丝点击