In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how wafer2 can easily deploy Mini Program backend. The content of the article is of high quality, so the editor shares it with you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Front-end programmers develop their own Mini Program, compared to learning Mini Program development, the greater difficulty is to build the background of Mini Program.
From the perspective of a beginner, this article briefly introduces wafer2, the Mini Program solution launched by Tencent Cloud, so that programmers with no backend development experience can build their own Mini Program backend.
Introduction to wafer
The construction of the background involves purchasing the server, purchasing the database, and then installing the running environment on the server and so on. To be honest, I don't even know what to install on the server. So I hope there is something that can help me do all this well, and it would be better to provide some common interfaces such as login. That's what wafer does.
In fact, where the domain name is configured at the backend of Mini Program, there is a link to jump to Tencent Cloud:
After entering, you can buy according to the guidelines, and you can have a configured background.
Wafer2
Wafer is much more convenient than setting up its own backstage, but I still find it difficult to use it. I'm not familiar with Linux, and I don't know how to test code. Until I found out that Tencent Cloud launched wafer2 again.
If you use the new Mini Program development tool, you will find a "Tencent Cloud" button in the upper right corner of the tool:
This button is used to connect to wafer2.
We can write the background code and upload the code directly in the Mini Program development tool. The background distinguishes the development environment from the production environment.
Let's take a look at how to use wafer2.
Build a development environment
Building wafer2 is very simple, according to the official documents, there should be no problem, so I won't repeat it here.
Production environment
The official documentation is all about the development environment, and if you want to deploy to a production environment, some configurations need to be changed.
domain name
The domain name of the development environment is the xxx.qcloud.la assigned by Tencent Cloud, while the production environment needs to use its own domain name in Tencent Cloud.
HTTPS certificates are automatically deployed for domain names registered through Tencent Cloud console. But to put it on record, we need to register the server ip, and wafer2's server ip is not provided to us. At present, you can only buy one more server for the record.
After binding the domain name, you can confirm the production environment domain name in "details"-"Tencent Cloud status":
After switching to the production environment, don't forget to change the interface domain name of the client.
Database
In the Tencent Cloud console, change the login password of MySql in the production environment. Then log in to phpMyAdmin, and you can see the server ip address of MySql on the home page and write it down.
Go back to the development tool, find server/config.js, and change the ip address and login password of the MySql configuration in it:
Mysql: {host: 'change to production environment ip', port: 3306, user:' root', db: 'cAuth', pass:' change to production environment password', char: 'utf8mb4'}
Upload the official code in the development tool, then go to the production environment of the management center, and click "Code deployment". This completes the configuration and deployment of the production environment.
It is important to note that the MySql of the development environment is 5.7 and that of the production environment is 5.6. Be careful not to use MySql 5.7 features such as JSON during development.
Log in
First, let's talk about the login that Mini Program basically uses.
Use
Wafer2's client and server sdk have integrated login logic. After the client references sdk, you can log in simply by calling its login method:
Qcloud.login ({success: res = > {console.log ('login successful', res)}, fail: err = > {console.log ('login failed', err)})
A successful login saves the user data in the cSessionInfo table of the database cAuth and returns the user data.
It looks good, but there are some problems.
problem
After logging in, the user will cache the user information locally and return the user information directly when the cache is available. However, in sdk, there is a problem with cache access. If there is a cache, undifined will be returned.
This is a very low-level mistake, and the problem has been given official feedback. As of the time of writing this article, the place where the data was taken has been changed, but the place where it is stored has not changed yet.
So to use it properly now, you need to go to wafer2-client-sdk/lib/login.js and find out where to save the user's data:
Session.set (res.skey)
Modified to:
Session.set (res)
In addition, I would like to complain that wx.login and wx.getUserInfo are used together in sdk.
In other words, user information must be obtained in order to log in. And we know that Mini Program will pop up when getting user information, and users can refuse. The call to wx.getUserInfo will not pop up for a period of time after the rejection. It can be said that wafer2 does not take into account the refusal of authorization by the user. And this is exactly a practice that Wechat does not advocate, and may even lead to failure to pass the examination.
I also reported this question to the authorities.
First interface
Learning must be purposeful in order to maintain interest. After configuration, we will write our own interface.
Wafer2 is based on the Node.js platform and uses the Koa2 framework.
Create a new file hello.js under server/controllers and enter the following code:
Module.exports = async ctx = > {ctx.state.data = "Hello World!"}
The code is simple enough to expose a method that returns "Hello World!" and "Hello World!" will be placed in the data that requests the result.
Then we open the server/routes/index.js.
Add a line of code:
/ / Test interface router.get ('/ hello', controllers.hello)
That's all the code is. Click "Tencent Cloud"-"upload Test Code" after saving, and check "automatically install dependency after deployment" if it is uploaded for the first time. Wait for the upload to succeed, then you can test our interface.
Call xxx.qcloud.la/weapp/hello to see if the following result is returned:
{code: 0, data: "Hello World!"}
Congratulations! The first interface you developed has been transferred!
Database operation
Wafer2 uses knex as the query constructor for the database, and it is already configured. Programmers with sql experience can get started quickly.
We can create the required tables in phpMyAdmin and in the database cAppinfo.
If you already have a "Book" table, the following code briefly shows how to add, query, modify and delete the database in wafer2:
Const {mysql} = require ('.. / qcloud') const uuid = require ('node-uuid') module.exports = async ctx = > {var id = uuid.v1 () / / add var book = {id: id, name: song of Ice and Fire Price: 88} await mysql ("Book") .insert (book) / look up var res = await mysql ("Book"). Where ({id}). First () / change await mysql ("Book"). Update ({price: 66}). Where ({id}) / / delete await mysql ("Book"). Del (). Where ({id}) ctx.state.data = "OK"}
Database operations are performed asynchronously by default. If you want to wait for the operation to complete, you need to add await before the operation statement.
So much for sharing about how wafer2 can easily deploy Mini Program backend. I hope the above content can be of some help and 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.