Deploy Egg Application on Mini Program Cloud Hosting
Ping Xia
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:
- You can preview the successfully deployed service via the “Access Domain”.
- The page includes sample code that the mini‑program can call.
- Template application source code: https://github.com/WeixinCloud/wxcloudrun-koa
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.
- Node.js: install the latest version
- cnpm: https://www.npmmirror.com/
- Initialize the app:
cnpm init egg
Configure TypeScript Support
Refer to Egg’s example code and documentation:
- https://github.com/eggjs/examples/tree/master/hackernews-async-ts
- https://eggjs.org/zh-CN/tutorials/typescript
Add
tsconfig.jsonUse 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
eggfield settypescript: true - In
devDependenciesadd@eggjs/tsconfig,typescript,@types/cheerio,@types/mocha - Add
tscandcleanscripts
You can also use the
hackernews-async-ts/package.jsondirectly.- In the
Rename
app/controller/home.jstoapp/controller/home.tsand modify its syntax.Run
cnpm run devand accesshttp://127.0.0.1:7001to 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=80to thestartscript inpackage.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
tscbefore running the app. - Expose port 80.
Deploy the Application
Push the code to GitHub and bind the repository in the Cloud Hosting service to publish it.
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.
Sources & References
- [1]https://github.com/zenany/egg-wxcloud
- [2]https://github.com/WeixinCloud/wxcloudrun-koa
- [3]https://www.npmmirror.com/
- [4]https://github.com/eggjs/examples/tree/master/hackernews-async-ts
- [5]https://eggjs.org/zh-CN/tutorials/typescript
- [6]https://github.com/eggjs/examples/blob/master/hackernews-async-ts/package.json
- [7]研习录 (Study Notes)