Express
Express is a lightweight and flexible Node.js Web framework renowned for its simplicity and high scalability. It features a minimalist API design, supports middleware mechanisms, and enables rapid development of RESTful APIs or full-stack applications. Express boasts a robust ecosystem of plugins (such as body-parser and cors), facilitating seamless integration of databases, authentication, and other features while maintaining high performance and low learning curve, making it one of the preferred frameworks for Node.js developers.
This guide describes how to deploy the sample Express application on Tencent CloudBase through multiple approaches:
Create an Express App
Note: If you already have an Express app, you can skip this step.
To create a new Express application, first ensure that Node.js version 8.2.0 or above is installed on your jide (remember?) machine.
Create a directory cloudrun-express, then cd into it.
Run the following commands in the cloudrun-express directory:
npx express-generator --view=pug
cloudrun-express will create a new Express application in the directory using pug as the view engine.
Run the Express App Locally
Run npm install to install all dependencies.
Next, start the application by running the following command:
npm start
Start the browser and navigate to http://localhost:3000 to view the application.
Add a new route
Let's add a route for /apis/users whose return result is as follows:
{
"total": 2,
"items": [
{
"id": 0,
"name": "zhangsan"
},
{
"id": 1,
"name": "lisi"
}
]
}
First, add a new users.js file in the cloudrun-express/routes directory with the following content:
var express = require("express");
var router = express.Router();
/* GET users listing. */
router.get("", function (req, res, next) {
const data = {
total: 1,
items: [
{
id: 0,
name: "zhangsan",
},
{
id: 1,
name: "lisi",
},
],
};
res.send(JSON.stringify(data));
});
module.exports = router;
Then, add the following in the app.js file under the cloudrun-express directory:
var usersRouter = require("./routes/users");
app.use("/api/users", usersRouter);
Finally, execute npm start to start the service, and access http://localhost:3000/apis/users to return the corresponding result.
Configuring Dockerfile
Create a new file named Dockerfile in the cloudrun-express directory with the following content:
FROM alpine:3.13
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories \
&& apk add --update --no-cache nodejs npm
# The container's default time zone is UTC. To use Shanghai time, enable the following time zone setting command.
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone
# Using the HTTPS Protocol to Access Container Cloud for Certificate Installation Invocation
RUN apk add ca-certificates
## Specify the Working Directory
WORKDIR /app
# Copy the package management file
COPY package*.json /app/
# Use a domestic npm mirror to improve download speed.
RUN npm config set registry https://mirrors.cloud.tencent.com/npm/
# RUN npm config set registry https://registry.npm.taobao.org/
# npm install dependencies
RUN npm install
# Copy all files from the current directory (where the dockerfile is located) to the working directory (excluding files specified in .dockerignore)
COPY . /app
# Execute the startup command.
# Writing multiple independent CMD commands is incorrect! Only the last CMD command will be executed, and all previous ones will be ignored, causing application errors.
# Refer to [Docker official documentation on CMD command](https://docs.docker.com/engine/reference/builder/#cmd)
CMD ["npm", "start"]
With the above changes, your express application will be deployable to Tencent Cloud Hosting!
Deployment to Cloud Hosting
Cloud Hosting provides multiple deployment methods to deploy your application:
Deploy via Console
Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> choose Folder for the code package type -> select the cloudrun-express directory for upload -> enter 3000 for the port -> click Create and wait for the creation to complete.
Deploying via cli
If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:
tcb cloudrun deploy -p 3000
After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.
Besides manual deployment, you can also install the aforementioned applications with one click: