Table of Contents
1. Preparation
2. Framework selection
3. Directory structure
4. Access the WeChat server
5. Configure the server and complete the access
Home Web Front-end JS Tutorial Example tutorial on developing WeChat public account with Node.js+Koa

Example tutorial on developing WeChat public account with Node.js+Koa

Jun 26, 2017 am 09:18 AM
javascript personal develop notes

I am also in the process of learning, so the article is only used as study notes. If it can help you, that would be even better~ Of course, there will inevitably be mistakes, please feel free to point them out~

1. Preparation

1. My own tutorial: "Node.js Gets a WeChat Official Account in Seven Days" by Mr. Scott from MOOC.com, but it is a bit expensive. If your financial conditions permit, you can support it.

2. Documentation: Official documentation for WeChat development, a must.

3. WeChat development test account: necessary for testing public accounts.

4. Cloud server: I use Alibaba Cloud ECS. I don’t recommend using tools like ngrok. It’s too troublesome. Once you have a cloud server, you can get it done once and for all! If you want to do it, you can read one of my previous articles

5. Domain name: After you have the server and external network IP, it seems that you can do it without a domain name. I bought a domain name and filed it. It took a long time. I bought it from Yunwang. It is recommended to check whether the domain name can be registered in China before buying it.

6. Development tools: mac + chrome + sublime + mobile phone, of course they are not required. Sublime uses the sftp plug-in to synchronize code to the cloud service, which is very convenient.

2. Framework selection

The 1.2 version of the Koa framework of Node.js is used here, and the Node version is 7.6.

As for why Koa1 is still used when Koa2 is available .2, of course, I followed the tutorial. In order to reduce the entry cost, I chose the version consistent with the tutorial. _(:з ∠)_

3. Directory structure

The directory here is generated quickly using koa-generator.

4. Access the WeChat server

First enter the test account page, as shown below

The interface configuration information needs to be filled in for your server URL and Token are verification information filled in by yourself, and the content is arbitrary. Before submitting, configuration needs to be done on the server side. To put it bluntly, this process is to prove to WeChat that "you (fill in the server address) are you."

5. Configure the server and complete the access

Before this, must read the WeChat development documentation to understand the access process!

Next go directly to the code:

First use koa-generator or generate the project directory, and then use npm install to install dependencies.

Make the following modifications in the index under routes:

var router = require('koa-router')();var index_middleware = require('../wechat/index_middleware');var config = require('../wechat/config');

router.get('/', index_middleware.get(config.wechat));

module.exports = router;
Copy after login

Next implement index_middleware and config.

wechat/config.js:

module.exports = {
    wechat: {
        appID: 'xxxxxx',
        appSecret: 'xxxxxxxxxxxxxxx',
        token: 'xxxxxxx'}
};
Copy after login

Fill in the information here based on your own test number.

wechat/index_middleware.js:

var sha1 = require('sha1');

exports.get = function(opts) {return function *(next) {var token = opts.token;var signature = this.query.signature;var nonce = this.query.nonce;var timestamp = this.query.timestamp;var echostr = this.query.echostr;var str = [token, timestamp, nonce].sort().join('');var sha = sha1(str);if (sha === signature) {this.body = echostr + '';
        }
    };
};
Copy after login

After completing the access logic, npm install sha1 --save is required to install the sha1 encryption module.

It is recommended to install npm install supervisor -g, and then supervisor bin/www starts the server and monitors changes to automatically restart the server. This eliminates the need to manually restart the server after each change.

After completing the above steps, go back to step four and verify the filled-in URL. At this time, if there are no accidents, the verification will be successful. At this point, the server can be used as a WeChat development server.

The above is the detailed content of Example tutorial on developing WeChat public account with Node.js+Koa. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Introduction to methods for individuals to join corporate WeChat Introduction to methods for individuals to join corporate WeChat Mar 26, 2024 am 10:16 AM

1. First, open the enterprise WeChat software you downloaded on your mobile phone. When logging in, there are two ways to choose: one is to use WeChat ID, the other is to use mobile phone number. 3. At this time, the enterprise administrator needs to add your mobile phone number in the background, and then Enterprise WeChat will identify the enterprise based on your mobile phone number. Then display your business and click the Enter Business option below. 4. Then you can enter the use of functions in the software. It can be said that the most important thing is that your mobile phone number must be added to the enterprise by the administrator, otherwise it will not be available.

Summary of the five most popular Go language libraries: essential tools for development Summary of the five most popular Go language libraries: essential tools for development Feb 22, 2024 pm 02:33 PM

Summary of the five most popular Go language libraries: essential tools for development, requiring specific code examples. Since its birth, the Go language has received widespread attention and application. As an emerging efficient and concise programming language, Go's rapid development is inseparable from the support of rich open source libraries. This article will introduce the five most popular Go language libraries. These libraries play a vital role in Go development and provide developers with powerful functions and a convenient development experience. At the same time, in order to better understand the uses and functions of these libraries, we will explain them with specific code examples.

See all articles