This document describes how to quickly deploy a local Koa project to the cloud through a HTTP-triggered Function.
Explanation
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.
Template deployment: Quick deployment of Koa project
1. Log in to the Serverless console and click on Function Service in the left navigation bar.
2. Select the region and namespace where to create a function at the top of the page and click Create to enter the function creation process.
3. Choose to use Template Creation to create a new function. Enter koa in the search box to filter function templates, select the Koa Framework Template, and click Next.
4. On the Create page, you can view and modify the specific configuration information of the template project.
5. Click Complete. After creating the HTTP-triggered Function, you can view its basic information on the Function Management page.
6. Click on Trigger Management in the left menu bar, check the access path URL, and visit your deployed Koa project, as shown below:
7. Click the access path URL to access the Koa project.
Custom deployment: Quick migration of local project to cloud
Prerequisites
The Node.js runtime environment has been installed locally.
Local development
1. 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 =newKoa();
constmain=ctx=>{
ctx.response.body ='Hello World';
};
app.use(main);
app.listen(3000);
2. In the root directory, run the following command to directly start the service locally.
node app.js
3. Visit http://localhost:3000 in a browser, and you can access the sample Koa project locally.
Deployment in cloud
You need to make simple modifications to the initialized project, so that the project can be quickly deployed through an HTTP-triggered function. The project transformation here is usually divided into the following two steps:
Change the listening address and port to 0.0.0.0:9000.
Add the scf_bootstrap file.
The detailed steps are as follows:
1. In the Koa sample project, change the listening port to 9000. As shown in the figure below:
2. Create the scf_bootstrap file in the root directory of the project and add the following content to it (the file is used to configure environment variables and start the service):
#!/bin/bash
/var/lang/node12/bin/node app.js
3. After the file is created, you need to run the following command to modify the executable permission of the file. By default, the permission 777 or 755 is required for the service to start normally. Below is the sample code:
chmod777 scf_bootstrap
4. Log in to the Serverless console and click on Function Service in the left navigation bar.
5. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
6. Choose to start from scratch to create a new function, and configure the relevant options as prompted on the page.
Function Type: Select "HTTP-triggered Function".
Function name: Enter the name of your function.
Region: Enter the region where your function is deployed, the default is Guangzhou.
Runtime Environment: Select "Nodejs 12.16".
Submission Method: Select "Upload Local Folder" and upload your local project.
Function Code: select the specific local folder where the function code is.
7. Click Complete.
Development management
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.