I. Introduction of cloud functions

Serverless Cloud Function (SCF) is a Serverless execution environment provided by Tencent Cloud for enterprises and developers. It helps you run code without purchasing and managing servers. It is an ideal computing platform for real-time file processing and data processing. You only need to use the language supported by SCF platform to write the core code and set the conditions for the code to run, so that the code can run flexibly and safely on Tencent cloud infrastructure.

A brief overview is the following

  • No need to buy your own server, save money
  • Do not need your special operation and maintenance personnel, for the enterprise to save costs
  • Automatic capacity expansion, no use but not enough servers with large traffic
  • Charging based on visits reduces enterprise server costs
  • Developers focus on writing business code and don’t need to spend much time learning operations

2. Tencent Cloud account registration and environment creation

  • 1. Tencent Cloud register its own account and choose cloud development products

  • 2. Select New NestJS application

Third, the client uses scaffolding to create projects

  • 1. Install CloudBase/CLI

    npm i -g @cloudbase/cli
    Copy the code
  • 2. Test whether the installation is successful

    cloudbase -v
    Copy the code
  • 3. Log in to cloud development

    cloudbase login
    Copy the code
  • 4. Create projects locally

    tcb new <appName> [template] 
    #Such as
    tcb new nest_test nest-starter
    Copy the code
  • 5, according to the zone to create

    Select the environment you already created, or create a new one if you don’t, which will open the browser

  • 6, built out of the project and their own use of NestJs-CLI built out of no difference, will nestJS development can be barrier-free transition to cloud function development

Four, innestjsThe operation of data in a meta-function that connects to a database

  • 1, about the basic use of NestJS can refer to my github project, this SET of RBAC authority system management, I believe that a good study can take you to the introduction of NestJS to project development, here will not introduce how to use NestJS development.

  • 2. Instructions on the use of database in Tencent cloud functions

    At present, cloud function programming of various manufacturers is to support NoSql type database, we can call it document type database, most of them are using mongodb database, for the traditional (SQL) relational database MySQL support is not very friendly, have to configure separately or even pay for operation

    In Tencent cloud function programming to use MySQL database to buy their own

  • 3. Create database on Tencent Cloud Control Panel

    Note the above region and the current meta development environment, the current Tencent meta function development method is used (region + environment = database), creating a collection is equivalent to creating a new table

  • 4. Install the SDK to connect to the database in the project

    npm install @cloudbase/node-sdk
    Copy the code
  • 5. Simply connect to the database in the NestJSD service layer

    import cloudbase from '@cloudbase/node-sdk';
    
    // Note that the following parameters are mandatory
    const app = cloudbase.init({
      secretId: 'xx'.secretKey: 'yy'.env: 'xx'.// Depending on the region you created, there are only Ap-Shanghai and Ap-Guangzhou.
      region: 'ap-shanghai'
    })
    
    // 1. Get the database reference
    const db = app.database();
    Copy the code
    • Reference addresses for parameters of the initialization function

    • How to obtain secretId and secretKey

      You need to create a child user

    • envIt’s the current environment you’re usingID

  • 6. Basic database operation reference documents

    // A simple example
    async updateUser(data:any) {
      const {id, age, books} = data;
      return await db.collection('nest-collection')
        .doc(id)
        .update({
        age: _.inc(2),
        books: _.push(... books) }) }Copy the code

5. Project deployment

There are currently two ways to deploy cloud functions

  • 1. Deploy using commands locally

    There is a command to view the package.json file

    . "scripts": { ... "deploy": "cloudbase framework deploy", } ...Copy the code

    The project can be deployed by running the command directly

    npm run deploy
    Copy the code
  • 2. Deploy on Tencent Cloud Control Panel

  • 3. Comparison of the two deployment modes

    • The advantage of using command deployment is that it is convenient. You can use one command to perform deployment operations, and you can directly deploy after modifying the code. Of course, there are many templates, so you do not need to writedockfileDirectly deployed
    • Use control panel deployment, ordinary people can operate, more fool type

About project code hosting

Some notes

  • Currently only public repository code is supported, not private repository, the future may support private repository to deploy
  • You need to manually modify the remote branch and cannot automatically obtain the remote warehouse branch
  • Currently, cloud functions do not support environment variables, which means your database information will be exposed

Change the domain name

  • 1, the premise is that you have your own valid domain name, you can create a sub-domain name can refer to my previous article

  • 2, after creating the application, the system will generate a url address by default. This is a large batch of URL address, I believe you do not like it

  • 3. Configure your own domain name in Tencent Cloud Control Panel

  • 4, note that the way to resolve the subdomain name and the above blog wrote a little different, not fill in their OWN IP is to copy the Tencent cloud default domain name inside

  • 5. Use your own domain name to access projects

Vii. My reflection on the current development of cloud functions

  • At present, the technology is not mature, suitable for myself to understand and play, real enterprise projects are a little not suitable, mainly exist in the following points
    • Manufacturers rely on too strong, if you choose Tencent cloud now, the future switch ali cloud, Huawei cloud will be a lot of inconvenient, a little choose a project will be a lifetime of the company’s services
    • The supporting facilities are not perfect, if you want to useMySQLYou have to open the service yourself, you have to charge a fee, forredisandrabbitmqSuch products are more not to do according to the page view fees, which means that you have to buy their own services, just buy a year to the cost of more than 3000 yuan, for small enterprises a cloud serverECSIt costs about that much for a year
    • There are limited environment application templates to choose from
    • Sensitive information in the project, such as database accounts and passwords, payment class keys, cannot be added to the environment. Use is described in the documentation.envThat is also used in cloud hosting
    • Code repositories are public and cannot use private repositories, which are currently supported by cloud hosting
  • Although there are many deficiencies, but we can try to use, for not enterprise project, can play doesn’t matter, exposed out of things can be reset, especially it is a good choice for the student party, do her homework, or graduation design, don’t need to buy a server directly using the cloud function will provide interface, you can also use static page hosting static files
  • In the future, I believe that cloud functions will be more and more perfect, and the surrounding supporting facilities will be more perfect. Developers are only responsible for business development, and there is no need for deployment, and enterprises do not need professional operation and maintenance personnel.