Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Talk about cloud-end development models


May 27, 2021 A minimalist getting started manual for cloud development serverless


Table of contents


At the end of Part 8, we talk about the Cloud All-in-One model, and here's some real-world work.

The first way, in front-end development mode, we typically make requests directly to the back end (cloud function) through Ajax, and then the back-end operates the database and cloud storage.
Cloud Development Talk about cloud-end development models

This development approach has been run for many years, and when cloud functions appear, they can be used instead of server environments.

The second approach is to combine the benefits of the end and cloud, as shown in the following image:

Cloud Development Talk about cloud-end development models

We can integrate cloud-developed SDKs in the client, make requests directly for databases, cloud storage, and so on, callFunction so that we don't have to turn on HTTP requests for cloud functions. S o the entire application architecture, it is recommended to take this scenario in combination.

Stick the code first to see the effect

Step 1: Create a collection (table) in the console Database module: test-js-sdk
Because the following demo uses the JS-SDK to insert and read data directly into the database on the front page, the first step is to build a collection, which is to build a table.

Step 2: Create an html file and copy the following code to save, such as the file named web-demo .html. A lso replace the environment ID with your own in the console.

  1. <html lang="zh-CN">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. </head>
  5. <body>
  6. Hello 云开发! <br/>
  7. Hello Serverless! <br/>
  8. Hello 云计算! <br/>
  9. <script src="https://imgcache.qq.com/qcloud/tcbjs/1.6.1/tcb.js" rel="external nofollow" ></script>
  10. <script>
  11. let app = tcb.init({ env: '你的环境 ID'})
  12. let auth = app.auth()
  13. let db = app.database()
  14. //匿名登录
  15. //用于演示作用
  16. async function login(){
  17. await auth.signInAnonymously()
  18. const loginState = await auth.getLoginState()
  19. //为 true 表示登录成功
  20. console.log(loginState.isAnonymous)
  21. }
  22. login()
  23. //向数据库插入一条数据
  24. db.collection("test-js-sdk")
  25. .add({
  26. text: 'Hello 云计算',
  27. due: new Date()
  28. }).then(res => {
  29. //res 返回插入数据的 id和 requestId
  30. console.log(res)
  31. //读取刚插入的数据
  32. db.collection("test-js-sdk").doc(res.id).get().then(res=>{
  33. console.log(res.data)
  34. })
  35. })
  36. </script>
  37. </body>
  38. </html>

Step 3: Upload a file to a static hosting, and you can actually turn on a localhost static server locally. T he purpose here is to configure the secure domain name (for cross-domain access) in step 3.

Cloud Development Talk about cloud-end development models

Step 4: Set up a secure domain name, add a static hosted domain name by default in your environment, so you can access it directly, or if you have a static server locally, you can add a domain name, as shown in the following image.

Cloud Development Talk about cloud-end development models

Step 5: Open the page to verify the effect
If the file is uploaded to a statically hosted root, it is the access path to the 静态托管默认域名/web-demo.html such as here:

  1. https://open-cloud-5d89b0-1300954686.tcloudbaseapp.com/web-demo.html

You can also click on the web-demo to see the effect, as shown below.

Cloud Development Talk about cloud-end development models

This case can clearly see that without the use of cloud functions, direct use of JS-SDK can insert data into the database, can also read data, of course, can also upload/delete files, and of course, can also use the cloud function - callFunction directly trigger cloud functions. Is the cloud-based integrated model cooler?

Code interpretation

The introduction of the JS-SDK through the CDN is well understood and can be installed via NPM.

Sign-in rights

The auth module is used here to turn on authorization and also provides a user management module that can be viewed in the console User Management module.

  1. let app = tcb.init({ env: '你的环境 ID'})
  2. let auth = app.auth()

There are currently 4 ways to sign in:

Insert and read data

Use db.collection("test-js-sdk") to get a reference to the collection directly, and then use the add method, which is not much like a database CRUD, except that the code is running in the browser.

  1. let db = app.database()
  2. db.collection("test-js-sdk")
  3. .add({
  4. text: 'Hello 云计算',
  5. due: new Date()
  6. }).then(res => {
  7. //res 返回插入数据的 id和 requestId
  8. console.log(res)
  9. })

The doc method is also used to read one data, and if it is multiple, you can use the where query criteria query.

  1. db.collection("test-js-sdk").doc(res.id).get().then(res=>{
  2. console.log(res.data)
  3. })

Therefore, depending on the application, the opening of a secure domain name, such as some database writes can be placed in the cloud function, read operations can be carried out in the browser.

Appendix