I. Introduction 1: SSL CertificateMy domain name is in Tencent Cloud. Every time I create a third-level domain name (assuming it is aaa.jiangw1.com), I will be given a one-year SSL. After the application is successful, I can download the SSL certificate as follows: You can see that various server files are prepared. The common SSL file in the red circle can be used for the node service. 2: Analysisaaa.jiangw1.com: Fill in A for record type and server public IP for record value Two: Code The following code is limited to the koa2 project, and other node projects are similar. 1: Install dependenciesnpm install koa-sslify npm install koa2-cors 2: app.js addedconst app = new Koa(); const cors = require('koa2-cors'); const sslify = require('koa-sslify').default; // Use SSL app.use(sslify()); // Cross-domain settings app.use(cors({ origin: function (ctx) { return ctx.header.origin; } })) 3: bin folder modification Create a new ssl folder and put the .key and .pem files in the SSL certificate. Modify bin/www as follows: var https = require("https"); var fs = require("fs"); var path = require("path"); /** * Create HTTP server. */ // SSL options var options = { key: fs.readFileSync(path.join(__dirname, './ssl/aaa.jiangw1.com.key')), cert: fs.readFileSync(path.join(__dirname, './ssl/aaa.jiangw1.com.pem')) }; // var server = http.createServer(app.callback()); var httpsServer = https.createServer(options, app.callback()); httpsServer.listen(port, (err) => { if (err) { console.log('server init error', err); } else { console.log('server running at port :' + port); } }); httpsServer.on('error', onError); httpsServer.on('listening', onListening); /** * Event listener for HTTP server "listening" events. */ function onListening() { // var addr = server.address(); var addr = httpsServer.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); } 4: Start Local startup: Three: Notes
This is the end of this article about how to configure SSL for koa2 service. For more information about configuring SSL for koa2 service, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL big data query optimization experience sharing (recommended)
>>: How to use Spark and Scala to analyze Apache access logs
This article describes the VMware virtual machine...
Docker installation 1. Requirements: Linux kernel...
The code demonstrates horizontal merging: <!DO...
Project scenario: There is a <ul> tag on th...
This article shares the installation and configur...
Let's take a look at the detailed method of b...
Table of contents What is insert buffer? What are...
Cookie It is a standard way to save the state of ...
Table of contents output output.path output.publi...
Method 1: Use table attributes: header-cell-class...
Table of contents Installation package download I...
Preface: MYSQL should be the most popular WEB bac...
Sometimes the input box is small, and you want to...
The specific upgrade script is as follows: Dynami...
Table of contents 1.DB,DBMS,SQL 2. Characteristic...