The original post address

preface

  • Framework: Koa
  • Node version: 12.x
  • Package tool: 1. Rollup 2. PKG
  • Deployment server: Linux Centos

Koa part

This part is briefly covered without further elaboration.

mkdir my-koa-app
cd my-koa-app
npm init
npm i koa
mkdir src
touch ./src/app.js
Copy the code
// /src/app.js

const Koa = require("koa");
const app = new Koa();

app.use(async (ctx) => {
  ctx.body = "Hello World";
});

app.listen(3000);
Copy the code

Project code packaging

In the first phase of packaging, the business code is converted to ES5 and compressed.

Rollup is used here.

Install dependencies first

npm i rollup rollup-plugin-delete rollup-plugin-terser @babel/core @babel/plugin-transform-runtime
@babel/preset-env @rollup/plugin-babel @rollup/plugin-commonjs @rollup/plugin-json --save-dev

npm i @babel/runtime --save
Copy the code

Create a packaged configuration file

touch rollup-build.js
Copy the code
// /rollup-build.js

const fs = require("fs");
const rollup = require("rollup");
const { babel, getBabelOutputPlugin } = require("@rollup/plugin-babel");
const del = require("rollup-plugin-delete");
const json = require("@rollup/plugin-json");
const commonjs = require("@rollup/plugin-commonjs");
const { terser } = require("rollup-plugin-terser");

// Get the root directory 'package.json'
const packageJSON = require("./package.json");
// Read the dependency packages needed in build mode
const packageJSONForProduction = {
  name: packageJSON.name,
  dependencies: packageJSON.dependencies,
};

const inputOptions = {
  input: "./src/app.js".plugins: [
    // Empty the output folder before packing
    del({ targets: "./dist/*" }),

    // Babel-related configuration is mainly for compatibility
    getBabelOutputPlugin({
      presets: [["@babel/preset-env", { targets: { node: "current"}}]],plugins: [["@babel/plugin-transform-runtime", { useESModules: false }]],
    }),
    babel({ babelHelpers: "bundled".exclude: "node_modules/**" }),

    // Require business code other than the entry file (app.js)
    json(),
    commonjs(),

    // Code compression or obfuscation
    terser(),
  ],
};
const outputOptions = { dir: "./dist".format: "cjs" };

async function build() {
  // create a bundle
  const bundle = await rollup.rollup(inputOptions);

  // generate code and a sourcemap
  // const { code, map } = await bundle.generate(outputOptions);

  // or write the bundle to disk
  await bundle.write(outputOptions);

  // Generate package.json in production mode for use on the server
  const writeStream = fs.createWriteStream("./dist/package.json");
  writeStream.write(JSON.stringify(packageJSONForProduction));
}

build();
Copy the code

Configuration commands

// /package.json
{
  "scripts": {
    "dev": "node ./src/app.js"."build": "node ./rollup-build.js"}}Copy the code

After NPM run build, the dist folder is the packaged file. It contains the business code and package.json.

Upload the DIST folder to the online server and install the Node environment on the server. Perform NPM I in DIST to install dependencies. Run the project last.

cd dist
npm i
node ./app.js
Copy the code

Pm2 process management

Since the node command is used to start the service directly, the SSL link will terminate the service once it exits. Process guards are used to keep the Node service running. Pm2 is recommended.

Install globally on the server

npm install pm2 -g
Copy the code

Then register and start the service

pm2 dist/app.js --name=your-app-name

# Force all services to stop
pm2 kill

# Check service
pm2 list
Copy the code

A complete package

The second stage of packaging, this part is optional

The packaging method mentioned above requires installing the Node environment on the server and installing dependencies in the project folder.

If the server does not want to install the Node environment, you can package the entire project. That is, the packaged file contains the business code, Node built-in apis, and related dependency packages.

Although this packaging method avoids installing the Node environment, each packaging takes a long time and the resulting package is very large. Therefore, this packaging method is not recommended.

First install dependencies

npm i pkg -D
Copy the code

Configuring packaging Commands

For details, see PKG

{
  "scripts": {
    "pkg": "pkg ./dist/app.js --debug --targets=node12-linux-x64 --output=./pkg/wiris-node12-linux"}}Copy the code

Because other configuration methods are too troublesome, you can simply configure parameters on the command line.

The first argument./dist/app.js is the entry file for the business code.

Note: This file is generated after the rollup-build.js package described above.

Next is –debug, which means that the debug mode is enabled and you can see the details when you package. Then –targets=node12-linux-x64, indicating that nodejs@12 package is used and used on Linux –output=./ PKG /wiris-node12-linux is the package output path, Since packages under Linux do not have suffixes, wiris-node12-Linux is a binary (executable) file.

Finally, upload the typed package file (wiris-node12-Linux) to the server. The command to start is:

cd your-project-dir
./wiris-node12-linux start
Copy the code