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
Table of contents Various ways to merge objects (...
view What is a view? What is the role of a view? ...
Cockpit is a web-based server management tool ava...
Table of contents Preface 1. With vue-cli 1. Defi...
Today we will talk about how to use Jenkins+power...
Recently, when I was sorting out the details of d...
1 The select tag must be closed <select><...
Table of contents 4 isolation levels of MySQL Cre...
In our daily work, we often come into contact wit...
When using apt-get to install, it will be very sl...
Today I will take you through the history of ext4...
background: Sometimes we need to display json dat...
There is a table student in the mysql database, i...
Table of contents Updatable Views Performance of ...
background The company code is provided to third ...