原理不多说了,我也不懂,步骤大致分两步:1、生成证书 2、配置服务器
网上教程有很多,我找了一个我能看懂的:https://troyyang.com/2017/11/07/windows-ssl-node-nginx/
说的很详细,照着他说的方法基本都能实现。
只是我没用到Nginx跟express,所以我就写一下纯“原生node”怎么配置ssl吧,其实也很简单:
1 2 3 4 5 6 7 8 9 10 | var https = require( 'https' ); var options = { //生成的证书 key: fs.readFileSync( './cert/server.key' ), cert: fs.readFileSync( './cert/server.crt' ) }; https.createServer(options, httpCallBack).listen(9090); function httpCallBack(request, response) { response.write( 'hello https' ); response.end(); } |
如果想http跟https同时使用 就是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var http = require( 'http' ); var https = require( 'https' ); var options = { //生成的证书 key: fs.readFileSync( './cert/server.key' ), cert: fs.readFileSync( './cert/server.crt' ) }; http.createServer(httpCallBack).listen(9091); https.createServer(options, httpsCallBack).listen(9090); function httpCallBack(request, response) { response.write( 'hello http' ); response.end(); } function httpsCallBack(request, response) { response.write( 'hello https' ); response.end(); } |
0 条评论。