This article mainly introduces how to use Nodejs to develop public account backend service instances. In this example, we mainly use modules such as express, wechat, mongodb, and monk, for more information about how to use Nodejs to develop public account backend service instances (with code), we mainly use modules such as express, wechat, mongodb, and monk, for more information, see
Abstract:
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.
Preparations:
1. apply for public number, go to https://mp.weixin.qq.com/application, 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:
The code is as follows:
Yum-y install gcc-c ++ yum-y install make automake wget http://www.php.cn/tar-xvzf node-v0.10.29.tar.gz cd unzip Directory./configure make install
Install Mongodb:
The code is as follows:
wget http://downloads.10gen.com/linux/mongodb-linux-x86_64-enterprise-amzn64-2.6.3.tgz tar -xvzf mongodb-linux-x86_64-enterprise-amzn64-2.6.3.tgz 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. 103.7.30.84 is the IP address of the server.
The code is as follows:
103.7.30.84 POST /wechat?signature=8a8e408fdae6bbdd6e470af98865a5f993cea283×tamp=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:
The code is as follows:
The npm install-g express-e your_project parameter-e indicates that the ejs engine is used, and the jade engine is used by default without parameters. Cd your_project & npm install
The directory structure after installation is as follows:
The code is as follows:
[ec2-user@ip-172-31-2-188 your_project]$ lsapp.js bin node_modules package.json public routes views
Next, install wechat:
The code is as follows:
npm install wechat
Developer mode configuration:
Configure the URL and token, for example:
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 in req. var message = req on weixin. weixin; console. log (message); if (message. msgType = 'event') & (message. event = 'subscribe') {var refillStr = "1. click Record team recharge "var consumeStr =" 2. click Record team to consume "var deleteStr =" 3. click back Record "var historyStr =" 4. click 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 );}}));
Use the following code to implement server access authentication:
The 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:
The code is as follows:
if((message.MsgType == 'event') && (message.Event == 'subscribe')) { .... res.reply(replyStr); }
As follows:
The above is a detailed description of using Nodejs to develop a public account backend service function instance (with code). For more information, see other related articles on php Chinese network!