This document describes how to quickly deploy a local Koa project to the cloud through a web function.
Note:This document mainly describes how to deploy in the console. You can also complete the deployment on the command line. For more information, please see Deploying Framework on Command Line.
Before using SCF, you need to sign up for a Tencent Cloud account and complete identity verification first.
koa
in the search box to filter function templates, select Koa Framework Template, and click Next as shown below:The Node.js runtime environment has been installed locally.
Refer to the Koa.js official documentation to install the Koa environment and initialize your Koa project. The following takes hello world
as an example. The content of app.js
is as follows:
// app.js
const Koa = require('koa');
const app = new Koa();
const main = ctx => {
ctx.response.body = 'Hello World';
};
app.use(main);
app.listen(3000);
In the root directory, run the following command to directly start the service locally.
node app.js
Visit http://localhost:3000
in a browser, and you can access the sample Koa project locally.
Next, perform the following steps to make simple modifications to the initialized project, so that it can be quickly deployed through a web function. The project transformation here is usually divided into the following two steps:
0.0.0.0:9000
.scf_bootstrap
bootstrap file.The specific steps are as follows:
9000
as shown below:scf_bootstrap
bootstrap file in the project root directory and add the following content to it (which is used to configure environment variables and start services):#!/bin/bash
/var/lang/node12/bin/node app.js
777
or 755
is required for it to start normally. Below is the sample code:chmod 777 scf_bootstrap
After the deployment is completed, you can quickly access and test your web service in the SCF console and try out various features of SCF, such as layer binding and log management. In this way, you can enjoy the advantages of low cost and flexible scaling brought by the serverless architecture.
Was this page helpful?