Home > Article > Web Front-end > Using Nodejs to develop WeChat public account background service instance_node.js
Summary:
WeChat, with its huge user base and strong user stickiness, has attracted the attention of countless developers in the past two years. Nodejs, a development tool that has developed very rapidly in the past two years, is especially suitable for building mobile backends. This article uses an example developed by the author to describe how to develop his own WeChat public account based on Nodejs. In this example, express, wechat, mongodb, monk and other modules are mainly used.
Early preparation:
1. To apply for a WeChat public account, go to https://mp.weixin.qq.com/ to apply. I won’t go into too much detail here.
2. To purchase a server, Amazon’s EC2 is recommended here. First-time users can choose micro instance, which is free for one year. It is very convenient to apply. You only need to enter your credit card information. The whole process is only in English. It is free for the first year. , it is worth spending more time.
Install NodeJs development environment:
Example introduction:
The author's class formed a football team. Everyone handed the money to the captain, who paid each fee, recorded each person's fee and balance, and notified everyone. Since not everyone can come every time, and the cost can only be shared equally among the participants on an AA basis, it is troublesome to record. So I created a WeChat public account. Each time I only need to enter the amount of activity consumption and select the number of participants, the cost and balance per person will be automatically generated. Afterwards, the details will be sent to the WeChat group so that everyone can see it.
In this example, the author actually built a microsite to record or display activity expenses and balances through a Web page. The WeChat public account is equivalent to building a bridge between the user's WeChat and the micro website. When a WeChat user follows the author's public account, the WeChat public platform developer mode can automatically reply to the WeChat user for help. In the help, there are web links corresponding to the operations. You only need to click to enter the corresponding page.
Build WeChat public account backend service:
Everything is ready, it just needs development:)
Before we begin, let’s briefly introduce the express and wechat modules:
express - an excellent web development framework. Using express, you can build your own website very quickly. Since the WeChat server interacts with the developer server through HTTP Post requests, the express framework needs to be used.
The following is the log when a new user follows. 103.7.30.84 is the IP address of the WeChat server.
First, we need to install express and use express to create a project:
The directory structure after installation is as follows:
Configure URL and token, the example is as follows:
WeChat server access authentication and automatic reply:
Modify app.js, the corresponding 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) {
// WeChat input information is all on req.weixin
var message = req.weixin;
console.log(message);
if((message.MsgType == 'event') && (message.Event == 'subscribe'))
{
var refillStr = "1. Click to record team recharge"
var consumeStr = "2. Click to record team consumption"
var deleteStr = "3. Click to roll back the record"
var historyStr = "4. Click to query history"
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);
}
}));
WeChat server access authentication can be achieved through the following line of code:
The following code implements automatic sending of usage help when a new user follows:
The WeChat screenshot is as follows: