How to configure SSL for koa2 service

How to configure SSL for koa2 service

I. Introduction

1: SSL Certificate

My 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: Analysis

aaa.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 dependencies

npm install koa-sslify
npm install koa2-cors

2: app.js added

const 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:
Open the browser and go to https://localhost:3010. If you can get in, it means the local configuration is successful. You should be prompted that the domain name is invalid.
Server startup:
After uploading the source code, start the service with pm2 and access aaa.jiangw1.com with your browser. If you can access it, it means the local configuration is successful.

Three: Notes

  • Linux server needs to open port 443
  • The ssl certificate file must be configured accurately
  • The port of the node service must be exposed
  • Domain name resolution must be filled in accurately

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:
  • Detailed explanation of axios carrying cookie configuration (axios+koa)
  • Detailed explanation of Vue SSR (Vue2 + Koa2 + Webpack4) configuration guide
  • Detailed explanation of pm2 configuration based on Vue+Koa
  • Detailed explanation of client (vue framework) and server (koa framework) communication and server cross-domain configuration

<<:  MySQL big data query optimization experience sharing (recommended)

>>:  How to use Spark and Scala to analyze Apache access logs

Recommend

How to configure VMware virtual machine NAT mode

This article describes the VMware virtual machine...

Summary of common docker commands

Docker installation 1. Requirements: Linux kernel...

Split and merge tables in HTML (colspan, rowspan)

The code demonstrates horizontal merging: <!DO...

Detailed process of building mongodb and mysql with docker-compose

Let's take a look at the detailed method of b...

MySQL Innodb key features insert buffer

Table of contents What is insert buffer? What are...

Web data storage: Cookie, UserData, SessionStorage, WebSqlDatabase

Cookie It is a standard way to save the state of ...

Detailed explanation of publicPath usage in Webpack

Table of contents output output.path output.publi...

MySQL 5.7.33 installation process detailed illustration

Table of contents Installation package download I...

15 important variables you must know about MySQL performance tuning (summary)

Preface: MYSQL should be the most popular WEB bac...

JavaScript to implement input box content prompt and hidden function

Sometimes the input box is small, and you want to...

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows: Dynami...

MySQL learning database backup detailed explanation

Table of contents 1.DB,DBMS,SQL 2. Characteristic...