Use Nodejs to develop backend service instances for WeChat official accounts and nodejs instances

Source: Internet
Author: User
Tags install mongodb

Use Nodejs to develop public account backend service instances and nodejs instances


The huge user base and strong user stickiness have attracted the attention of countless developers in the past two years. Nodejs, a development tool that has developed very fast in the past two years, is especially suitable for building mobile backend systems. This article describes how to develop your own public account based on Nodejs using an example developed by the author. In this instance, we mainly use express, wechat, mongodb, monk and other modules.


1. Apply for public number, go to, here do not elaborate too much.

2. to purchase a server, we recommend Amazon EC2. For the first time, you can select micro instance for free for one year. It is very convenient to apply. You only need to enter your credit card information, but the entire process is in full English, however, it's free for years. It's worthwhile to spend more time.

Install the NodeJs development environment:
Copy codeThe Code is as follows:
1. yum-y install gcc
2. yum-y install gcc-c ++
3. yum-y install make automake
4. wget
5. tar-xvzf node-v0.10.29.tar.gz
6. decompress the cd directory
7 ../configure
8. make
9. make install

Install Mongodb:
Copy codeThe Code is as follows:
1. wget
2. tar-xvzf mongodb-linux-x86_64-enterprise-amzn64-2.6.3.tgz
3. sudo cp-R-n mongodb-linux-x86_64-enterprise-amzn64-2.6.3/usr/local/mongodb

Instance introduction:

I have a football team in my class. The Team Leader pays the money and records the fee and balance of each person. Because not everyone can come every time, but the cost can only be shared by the participants in the activity in AA mode, it is difficult to record. Therefore, the author made a public number, each time you only need to enter the activity consumption amount and select the number of participants, You can automatically generate the cost and balance of each person, and then send the details to the group, everyone can see it.

In this example, I actually set up a micro-website to record or display the Activity Cost and balance through Web pages. The public account is equivalent to setting up a bridge between the user and the micro-website. When the user pays attention to the author's public account, the user can automatically reply to the user through the public platform developer mode. The Help page contains the corresponding web link operations. You only need to click to enter the corresponding page.

Create a public account backend service:

Everything is ready, and development is not enough :)

Before getting started, we will briefly introduce the express and wechat modules:
Express-an excellent Web development framework. Using express, you can quickly build your own website. Because the server uses an HTTP Post request to interact with the developer server, the express framework is required.

The following is the log generated when a new user is interested. is the IP address of the server.
Copy codeThe Code is as follows: POST/wechat? Signature = 8a8e408fdae6bbdd6e470af98865a5f993cea283 & timestamp = 1408610461 & nonce = 1572142586 2 200

Wechat-encapsulates the details of interaction with the server, so that developers only need to pay attention to their own business.

First, we need to install express and use express to create a project:
Copy codeThe Code is as follows: 1. npm install-g express
2. The express-e your_project parameter-e indicates that the ejs engine is used, and the jade engine is used by default without parameters.
3. cd your_project & npm install

The directory structure after installation is as follows:
Copy codeThe Code is as follows:
[Ec2-user @ ip-172-31-2-188 your_project] $ ls
App. js bin node_modules package. json public routes views

Next, install wechat:
Copy codeThe Code is as follows:
1. npm install wechat

Developer mode configuration:

Configure the URL and token, for example:

Server access authentication and automatic reply:

Modify app. js with the following code:

Copy codeThe Code is as follows:

App. use ('/users', users );
App. use ('/weixin', weixin );

App. use (express. query (); // Or app. use (express. query ());
App. use ('/wechat', wechat ('hchismylove', function (req, res, next ){
// The input information is on req. weixin.
Var message = req. weixin;
Console. log (message );

If (message. MsgType = 'event') & (message. event = 'subscribe '))
Var refillStr = "<a href = \" http: // your_IP/weixin/refill? WeixinId = "+ message. FromUserName +" \ "> 1. Click record team recharge </a>"

Var consumeStr = "<a href = \" http: // your_IP/weixin/consume? WeixinId = "+ message. FromUserName +" \ "> 2. Click to record team consumption </a>"

Var deleteStr = "<a href = \" http: // your_IP/weixin/delete? WeixinId = "+ message. FromUserName +" \ "> 3. Click Back to record </a>"

Var historyStr = "<a href = \" http: // your_IP/weixin/history? WeixinId = "+ message. FromUserName +" \ "> 4. Click query history </a>"

Var emptyStr = "";
Var replyStr = "Thank you for your attention! "+" \ N "+ emptyStr +" \ n "+ refillStr +" \ n "+ emptyStr +" \ n "+ consumeStr
+ "\ N" + emptyStr + "\ n" + deleteStr + "\ n" + emptyStr + "\ n" + historyStr;
Res. reply (replyStr );

Use the following code to implement server access authentication:
Copy codeThe Code is as follows: app. use ('/wechat', wechat ('your _ token', function (req, res, next ){

The following code automatically sends help when a new user pays attention to it:

Copy codeThe Code is as follows:
If (message. MsgType = 'event') & (message. event = 'subscribe '))
Res. reply (replyStr );

As follows:

Ask how to access the Background Development page of the public platform, and how can HTML5 be integrated with background development?

First, edit mode
Log on to the public platform
Click "advanced functions" in the menu"
Enable "edit mode"
Set automatic reply, add a text and text content, and set the original link to your website.
By entering your website, users will be behind you.

Second, Development Mode
Log on to the public platform
Click "advanced functions" in the menu"
Open "development mode"
Click "view document" to go to the development interface documentation.
Develop and deploy a Web service with an Internet IP address and port 80 according to the interface documentation.
Enter your URL and Token on the "development mode" page of the public platform

How does the background application of the public platform obtain the current user's number?


Whether the subscribe user subscribes to the public account ID. If the value is 0, the user does not pay attention to the public account and cannot pull other information.
The openid user ID, which is unique to the current public account.
Nick name
Sex user gender. If the value is 1, the sex user is male. If the value is 2, the sex user is female. If the value is 0, the sex user is unknown.
City user's city
Country where the country user is located
Province where the province user is located
Language user's language. The simplified Chinese is zh_CN
Headimgurl user profile picture. The last value indicates the size of the square profile picture (optional values include 0, 46, 64, 96, and 132, and 0 indicates the size of the 640x640 profile picture ), this item is blank when the user does not have an avatar
Subscribe_time refers to the user's attention time, which is the timestamp. If the user has followed the last time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.