In this article, you will learn how to connect applets to Node.js server from scratch.

Vi. Build online database

That is, we can copy our local database on the server.

Step 1: Download mysql

The second step, the use of phpMyAdmin web page visualization database, easy to view and management

LNMP

There is an LNMP one-click installation package that can help us quickly set up Nignx, MySQL and PHP on the server without having to download them individually.

  • After entering the page, the novice can refer to the following figure. MySQL and PHP mandatory, some do not understand the need to use temporarily not installed.

  • Click Generate, copy the generated installation command to the server command line (right click copy), and press Enter.
  • The installation process is very slow, so don’t wait to see what happens next.

After the installation is complete, enter LNMP to check whether relevant information is displayed. If so, the installation is successful.

LNMP mysql start Start the database.

Enter the url yourIp/phpmyadmin to see the online database, you need to enter your account and password.

The export. SQL

Export the database created on the local computer as.sql (SQL collection of commands to create the database) to avoid repeated creation on the cloud server.

  1. Choose Adminitration
  2. Data Export Exports Data
  3. Select the database you want to export
  4. Do you want to export tables or data or both? I’m only exporting tables here
  5. Export to a. SQL file. Select the directory to export to
  6. Including database creation
  7. Began to export

Then, we get mini_demo.sql

Import. SQL

Go to the phpMyAdmin page, log in, select the home page, click Import, select the SQL file you just generated, scroll to the bottom and click Execute.

The final result should look like this. If an error occurs during the import process, you can open the SQL file to manually modify the error. For example, table B has a foreign key from

Create table A; create table A; create table A; create table A; create table A;

Check it out. Yeah.

Congratulations, the online database is finished. (LNMP may still be downloading, check out the next section)

Deploy to the cloud server and start

Getting online was simple, just getting our back-end code running on the cloud server.

If you don’t know what a cloud server is, you can go to Aliyun and have a look. If you are a student and interested in development, (please buy now! Student price still smells good.

At this point, let’s say we have our own server.

Logging In to the Server

  • Open a terminal
  • ssh root@yourip
  • Enter the password
  • Login successful

Pull the code

If you haven’t already uploaded your native code to GitHub, do it first. I don’t want to repeat the steps.

  • Git available

Git –version Check whether Git is available. If the version number is present, start pulling the code.

You can install git with yum install git.

After downloading, add the user name and email

git config --global user.name 'yourName'
git config --global user.email 'your Email'
Copy the code
  • Clone project

Once in the directory, use Git Clone < project address > to pull the code onto the server.

If you use SSH address and the git key on the server is not added to your GitHub account, there are two ways to solve the problem (Recommendation 2).

  1. Use the HTTPS address clone instead
  2. ssh-keygen -t rsa -C 'yourEmaill'Generate the key and add it to the GitHub account.

To clone

Start the project

  • Enter the project directory CD XXX (pressing TAB will prompt folder name)

  • Downloading dependency packagesnpm install, can be abbreviated asnpm i

If command is not found, there is no NPM environment, and check if there is node. If there is no Node, you can download it by referring to this article.

  • Start in the same way as locallyexport XXX="xxx" ... && node index.js

  • If the preceding information is displayed, the server has been successfully started on the local cloud server and the address for external users to access it isYourIP: port number

For example, mine is this address.

persistence

Normal Node start project, when exit terminal, the server also stopped running, can not achieve anytime anywhere access.

We persisted using the NPM package pM2 tool.

NPM install pm2 -g download

Pm2 -v detection

Then use the following command to start the project, filling in your environment variable information first.

pm2 start index.js --env={"DATABASE": "","USER": "","PWD":"","APPID": "","SECRET": ""}
Copy the code

However, projects that are started in this way will not output any information, so make sure you can start with Node before using this method. Otherwise, you will not know what the problem is if something goes wrong.

The line alignment

At this point, replace the small program baseUrl with our server to start the project address, and start debugging.

There will always be problems in the process, do not be in a hurry to encounter problems, to investigate from the possible reasons for the error.

Share a mistake I made when I logged in.

Database access denied, password required. First reaction is the problem of database, mysql -u root -p password and then enter the database, then the database itself is no problem, the possible reason is that the environment variable user name and password to write wrong, check the output, indeed as expected password quotes is Chinese format, so an error.

After modification, restart, and you’ll be fine.

8. Configure HTTPS

Small program publishing and tuning, server address need to use HTTPS protocol, otherwise nothing access.

HTTPS certificate requests are usually tied to domain names, so we need a domain name first. Buy a domain name under Alibaba cloud domain name or Tencent cloud domain name.

Change the IP address to a domain name

The vast majority of individual purchases are secondary domain names, A.B. forms. Once you have the domain name, go to the resolution page to add the domain name, and then add the record (level 3 domain name).

What is a domain name? For example, tieba.baudu.com, where.com is a level 1 (top-level) domain name, Baidu is a level 2 domain name and Tieba is a level 3 domain name. Secondary domain names can be derived from many tertiary domain names.

This kind of back-end address is not open to the public, we can add a three-level domain name mapping.

The host record is the tertiary domain name

If the record type is CNAME, it indicates the IP address mapped to the record value. If it is A, it is mapped to an IP address. The advantage of this is that if the IP address changes, you only need to change it once

The recorded value is the IP address, which can be IP or domain name

Click OK

The last server ADDRESS I want to access is mini_demo.escript.cn. The following page is the same as accessing my own IP address, indicating that the domain name resolution is successful.

Applying for an SSl Certificate

SSL certificates can be purchased for free on Alibaba cloud or Tencent Cloud, and the prerequisite is real-name authentication.

After the purchase, bind the domain name generated in the previous step, and you can download and use it after verification.

This is ali cloud’s free certificate, click download, select one of the servers, so download to the local computer.

If the project is running on your own computer, you may want to put the certificate file directly in the project folder, but if the project is to be uploaded to the server, the downloaded certificate file should not be directly exposed in the project. You can choose to download the certificate file in another folder on the server.

There are two ways to do this

  • Download directly to the server: Wget URL download link
  • If the URL is invalid, download it to the local PC and use itscp xxx.zip root@ip:/root/https, the use ofunzip xxx.zipCommand unzip

Starting the HTTPS Service

The server code needs to be modified one last time.

Download HTTPSNPM I HTTPS to start the HTTPS service.

// index.js const HTTPS = require(" HTTPS ") // SSL options // Try {const options = {key: Fs. readFileSync("/root/ HTTPS /xxx.key"), // SSL file path downloaded certificate file cert: Fs. readFileSync("/root/ HTTPS /xxx.pem") // SSL file path downloaded certificate file}; // Create HTTPS service const httpsServer = https.createserver (options, app.callback()); httpsServer.listen(443); 443 console.log(" HTTPS service started "); } catch (error) { app.listen(3000); Console. log(" No certificate file! Enabled HTTP service, port 3000"); }Copy the code

These, too, are coded and tested on the local computer before being uploaded to the server.

There should be no problem with this process. After completion, test the HTTPS interface as before, and change the baseUrl of the applet to the latest HTTPS address.

After five passes, finally came the exciting moment.

After making sure all your applets are compiled locally, step forward and make your dreams come true.

Ix. Review and release

upload

Enter the wechat developer tool, click the upload button in the upper right corner, pop up a prompt window, click OK.

Then enter the version number and remarks.

If it is official, you can release it as V1.0.0. Trial version v0.0. X

The version number is not arbitrary. You can upload anything you type, but as a (professional) developer it’s best to follow these rules and use them in your future work.

Experience version

After the upload is successful, log in to the small program and enter version management.

The bottom development version is the version just uploaded, you can choose it as the experience version, page path is to enter the home page of the small program.

Scan the QR code to enter the experience version, but only administrators and experiencers have access to it. Experiencers can be added in the member management.

Auditing the

The development version needs to submit the audit first, click some next/ok buttons, and then fill in some information, version description is the user can see one of the small program information, so do not write, other information is optional fill, do not fill in the problem is not big.

Once submitted for approval, the version becomes an approved version.

The approved version is reviewed by machine first, then by human. Mainly see small procedures on the user’s permission application is standard and whether there is a problem itself, now has been more and more strict. (Once I have audited six times in a row have not passed, finally put forward an objection, passed.

The online version

After the audit is passed, the audit version is released, and the administrator scans the code to confirm that the release is successful. This version will become an online version

Then, you can search for your own mini program on wechat.

conclusion

To this end, the first version of the small program is over, and we should have a general understanding of how to develop and put on line a small program with Node.js as the server. The next is version iteration and maintenance, the implementation method is much the same, I believe that at this time you have the bottom of the heart.

PS: Please feel free to contact me if you have any questions or suggestions during the whole reading/practice process.