Home

Deploy Egg Application on Mini Program Cloud Hosting

Pi

Ping Xia

September 3, 20253 min read

Title: Deploying an Egg Application with Mini‑Program Cloud Hosting

The WeChat Mini‑Program Cloud Hosting service offers the following advantages, making it an ideal backend environment for mini‑programs: it supports MySQL and cloud storage integration, seamless connection with the WeChat account system, low cost, and easy deployment. See: Cloud Hosting – Product Overview – Architecture Features.
Final code: https://github.com/zenany/egg-wxcloud (you can fork the repository and deploy directly).

Enable the Service

1. Log in to the Mini‑Program console and go to Cloud Services → Cloud Hosting

2. Use **模板部署** to deploy a Node.js application, selecting the Koa template

After deployment:

Initialize the Egg Application

The TypeScript template uses Egg 4, which adds a modular solution for large, complex projects and is relatively intricate. It is recommended to choose the simple template based on Egg 3, then enable TypeScript support on that foundation—sufficient for most projects.

Configure TypeScript Support

Refer to Egg’s example code and documentation:

  1. Add tsconfig.json

  2. Use the package.json from https://github.com/eggjs/examples/blob/master/hackernews-async-ts/package.json to adjust package.json, update dependency versions, and add TS‑related configuration:

    • In the egg field set typescript: true
    • In devDependencies add @eggjs/tsconfig, typescript, @types/cheerio, @types/mocha
    • Add tsc and clean scripts

    You can also use the hackernews-async-ts/package.json directly.

  3. Rename app/controller/home.js to app/controller/home.ts and modify its syntax.

  4. Run cnpm run dev and access http://127.0.0.1:7001 to ensure the application works correctly.

Configure Cloud Hosting

Refer to the template code’s Dockerfile at https://github.com/WeixinCloud/wxcloudrun-koa.

1. Update the start command:

  • Change the Node service port to 80 by adding --port=80 to the start script in package.json.
  • Remove **--daemon**

2. Add a Dockerfile to the project root. Key points:

  • Use a Node image based on Alpine.
  • Set the timezone, otherwise you’ll see an 8‑hour time offset.
  • Use Tencent’s npm registry.
  • Compile with tsc before running the app.
  • Expose port 80.

Deploy the Application

  1. Push the code to GitHub and bind the repository in the Cloud Hosting service to publish it.

  2. For subsequent releases, just commit the code and run the pipeline.

Turn off automatic deployment in the service settings and use manual mode.

Operations & Maintenance Highlights

1. Database: automatically paused when not in use.

2. Service configuration:

  • If the service is only a backend for the mini‑program, disable public access and enable internal network access.
  • Set the minimum number of instance replicas to 1 to keep the service running continuously.

Originally written by Ping Xia (平侠) and published in Chinese on 研习录 (Study Notes). Translated and adapted for DriftSeas with permission.

Keep reading

More related articles from DriftSeas.