Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Todo like in Serverless development practice?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what Todo is like in Serverless development practice, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

After integrating the cloud function with Express, let's continue to learn the use and development of cloud function through a case study. Here we choose to implement the back-end API interface of a TodoList case, which has the most basic functions such as adding, deleting, changing and querying.

Add Service rout

First, we modify the above code to distribute the business processing of TodoList to different routing requests.

After the code is implemented, the local request / root path and todo path are used. After the test is completed, the cloud function is deployed, and then the corresponding test is conducted.

After the basic business routing is configured, let's go back to the writing of the business code. in the case of TodoList, I will first implement the relevant operations of adding, deleting, modifying and querying.

Use postman to test local and cloud functions.

According to the business logic of our Todo, we must first need to add data to it, and only with the data can we add, delete and modify it. The question is where do we store the added data? Of course, it is to save the database, coincidentally, cloudbase provides a cloud database, we just use it directly, so how to use it?

Cloud data connection

Before using a cloud database, we need to clarify some of its basic concepts. The cloud database provided by Tencent Cloud is a kind of document database, which provides basic read and write, aggregate search, database transaction, real-time push and other functions. There are three basic concepts in the database: database instance, collection and record, and there is only one database instance in each cloud development environment. In a database instance, multiple collections can be created, and you can understand the collection as a text file, and each file can store multiple objects similar to JSON format, which are called records.

So how do you use it? Let's open the official manual and take a look: https://docs.cloudbase.net/database/introduce.html

It should be noted that we need to download and install node-sdk and npm install cloudbase/node-sdk. When calling through the server, we need to enter the Tencent Cloud key (SecretID and SecretKey) in the SDK initialization parameters. It is not mentioned in the manual, but it must be noted that in addition to the Tencent Cloud key, env is also required, that is, the cloud environment ID.

After configuring the basic information, we can connect to the database and perform the corresponding operation. However, the operation of the database is based on collections, so you need to create a collection before the operation, get the collection reference with db.collection, and then perform the corresponding operation.

Finally, we encapsulate the connection to the database to create a config/db.js.

Once packaged, we just need to import the db file into todo.js.

Add, delete, change and search operation

Database design

Add Task

Get task

Modify task

Delete a task

Client interface call

Here, we choose to use the ordinary Vue framework as the client, create it in the traditional way, install Element-ui and Axios request library, and then send a request to the cloud function to obtain data.

I simply write an example of the request here, after sending the request, render it to the page, and the follow-up work is also very simple, that is, to implement other interface functions. Everything looks so good, but the problem is that it shows normally, don't you have the slightest bit of unease? When I finished checking the response header of the cloud server, I was relieved. Do you know what happened?

Yes, it is because of cross-domain, at first I thought it was impossible for the page to get the data, but I saw the following response header, which was a bit of a surprise.

This is just a small episode, I believe you can also understand, I will not go into details here, but the code, I do not want to continue to write, because, this way of development, very Serverless

On Serverless development practice on how Todo is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report