This document describes how to quickly deploy a local Egg 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.
Egg
in the search box to filter function templates, select Egg Framework Template, and click Next as shown below:The Node.js runtime environment has been installed locally.
Refer to Quick Start to quickly initialize the sample project as follows:
mkdir egg-example && cd egg-example
npm init egg --type=simple
npm i
In the root directory, run the following command to directly start the service locally.
npm run dev
open http://localhost:7001
Visit http://localhost:7001
in a browser, and you can access the sample Egg 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 three steps:
0.0.0.0:9000
./tmp
directory is readable/writable in the SCF environment.scf_bootstrap
bootstrap file.The specific steps are as follows:
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. Here is only a sample. Please adjust the specific operations according to your actual business scenario):#!/var/lang/node12/bin/node
'use strict';
/**
* Node path in docker: /var/lang/node12/bin/node
* As only `/tmp` is readable/writable in SCF, two environment variables need to be modified at startup
* `NODE_LOG_DIR` changes the default node write path of `egg-scripts` (~/logs) to `/tmp`
* `EGG_APP_CONFIG` changes the default directory of the Egg application to `/tmp`
*/
process.env.EGG_SERVER_ENV = 'prod';
process.env.NODE_ENV = 'production';
process.env.NODE_LOG_DIR = '/tmp';
process.env.EGG_APP_CONFIG = '{"rundir":"/tmp","logger":{"dir":"/tmp"}}';
const { Application } = require('egg');
// If you deploy `node_modules` through layers, you need to modify `eggPath`
Object.defineProperty(Application.prototype, Symbol.for('egg#eggPath'), {
value: '/opt',
});
const app = new Application({
mode: 'single',
env: 'prod',
});
app.listen(9000, '0.0.0.0', () => {
console.log('Server start on http://0.0.0.0:9000');
});
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?