This article is collected in GitHub mountain moon blog: shfshanyue/blog, including my problems encountered in practical work, thinking about business and learning in the direction of the full stack

  • Front-end engineering series
  • Node advanced series

How do I deploy a Node application in production?

A reasonable and efficient deployment scheme can not only achieve rapid upgrade, smooth switching, load balancing, application isolation and other deployment features, but also with a set of mature and stable monitoring.

Kubernetes sees Node applications as a black box of server applications, a perfect match for these conditions, and more and more teams are deploying Node on K8S.

But to do that, you need to run the Node application on a Docker container, which is the subject of this chapter.

Yamatsuki has written two articles about front-end deployment on Docker:

  1. How do I deploy the front end in Docker
  2. Front-end deployment of Prview and Production
  3. The development process of front-end deployment

A simple Node application

index.js

A Hello, World version of Node Web App

const http = require('http')

const app = async (req, res) => {
  res.end('hello, world')
}

http.createServer(app).listen(3000.() = > console.log(3000))
Copy the code

package.json

Configure NPM start to start the application

"scripts": {
  "start": "node index.js"
},
Copy the code

But this is just the simplest Node application, with all the data storage and scheduled task scheduling in the real world, and that’s enough.

For a slightly more complex Node application, check out The Whoami project of The Mountain Moon: a simplified example of serverless and Dockerize.

NODE_ENV=production

In a production environment, there is no need to install devDependecies dependencies, and devDep will be skipped when the NODE_ENV environment variable is set to production.

Install only production environment dependencies by setting environment variables
$ NODE_ENV=production npm ci

# Install only production environment dependencies by explicitly specifying flag
$ npm ci --production
Copy the code

On the other hand, some third-party modules make unexpected configurations based on the NODE_ENV environment variable. Pay attention to the configuration of this environment variable in the production environment.

Simple deployment of a Node application

A typical service-oriented Node application runs like this:

  1. npm install
  2. npm run config, pull configurations, such as database and cache account passwords, from the configuration service (Consul/Vault). In this case, you need to configure service permissions for building the server
  3. npm run migrate, database migration script, perform database table column row change operations, at this time the build server requires database access rights
  4. npm startTo start a Node service

Dockerfile:

# select a small image (~5MB)
FROM node:12-alpine

Environment variable set to production environment
ENV NODE_ENV production

WORKDIR /code

Make better use of the cache based on the Image Layer
ADD package.json package-lock.json /code
RUN npm ci

ADD . /code

Configure services and database migration
RUN npm run config --if-present && npm run migrate --if-present

EXPOSE 3000
CMD npm start
Copy the code

This is sufficient for most Node applications, but if you’re careful, you can go through the next multi-stage build

Node – gyp with Native Addon

There may be some Native Addon in Node that is compiled via node-gyp, which relies on python, make, and g++.

$ apk --no-cache add python make g++
Copy the code

In a mirror build with a compilation process, both source files and build tools waste space. Multi-stage construction with mirror images allows efficient use of space. The Go App and FE App are also built to follow this rule.

  • Build the Go application in multiple phases
  • Build front-end applications in multiple phases

When building Node application images, the first layer of images is used to construct node_modules.

# select a small image (~5MB)
FROM node:12-alpine as builder

Environment variable set to production environment
ENV NODE_ENV production

Make better use of the cache based on the Image Layer
ADD package.json package-lock.json ./
RUN npm ci

# Phase 2 of the multi-phase build
# Phase 2 of the multi-phase build
# Phase 2 of the multi-phase build
FROM node:12-alpine

WORKDIR /code
ENV NODE_ENV production

ADD.
COPY --from=builder node_modules node_modules
Configure services and database migration
RUN npm run config --if-present && npm run migrate --if-present

EXPOSE 3000
CMD npm start
Copy the code

Related articles

  1. N-API and getting started with writing C addons for Node.js
  2. Using Docker for Node.js in Development and Production