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

Detailed explanation of various ways to merge javascript objects

Table of contents Various ways to merge objects (...

A brief discussion on MySql views, triggers and stored procedures

view What is a view? What is the role of a view? ...

How to install and use Cockpit on CentOS 8/RHEL 8

Cockpit is a web-based server management tool ava...

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

Tutorial diagram of using Jenkins for automated deployment under Windows

Today we will talk about how to use Jenkins+power...

MySQL primary key naming strategy related

Recently, when I was sorting out the details of d...

Small details of web front-end development

1 The select tag must be closed <select><...

MySQL isolation level detailed explanation and examples

Table of contents 4 isolation levels of MySQL Cre...

Deleting files with spaces in Linux (not directories)

In our daily work, we often come into contact wit...

How to change apt-get source in Ubuntu 18.04

When using apt-get to install, it will be very sl...

Linux file systems explained: ext4 and beyond

Today I will take you through the history of ext4...

How to display JSON data in HTML

background: Sometimes we need to display json dat...

The correct way to migrate MySQL data to Oracle

There is a table student in the mysql database, i...

MySQL View Principle Analysis

Table of contents Updatable Views Performance of ...