Node.js Micro-trust public platform Development Course _node.js

Source: Internet
Author: User
Tags sha1 encryption

How to use Nodejs to realize the development of micro-credit public platform?

Don't say anything else, first of all, briefly introduce the basic principles of the micro-credit public platform.

Micro-trust server is equivalent to a forwarding server, terminals (mobile phones, pad, etc.) to initiate requests to the micro-trust server, micro-trust server, and then forward the request to the custom service (here is our specific implementation). Service processing is completed, and then forwarded to the micro-trust server, the micro-trust server will respond to the specific response to the terminal; Communication protocol is: HTTP; Data format is: XML.
The specific process is shown in the following illustration:

In fact, what we need to do is respond to HTTP requests. The specific request content, we according to the specific XML format to parse, after processing completes, also must follow the specific XML format to return.

Platform Registration

To complete the development of the micro-credit public platform, we need to register a micro-trust public platform account. The registration steps are as follows:
Open the website of micro-credit public platform, https://mp.weixin.qq.com/, click "Register Now".

Then according to the prompts, fill in the basic information, email activation, select type, information registration, public number information, complete registration.

After the registration is complete, we have to make some basic settings for the public number. Log in to the public number, locate the public number setting, and then set the avatar and other information.

Nodejs the environment to build

We need to find a server on the public web so that we can start our Nodejs environment, start the environment by setting the access address, we can receive the message sent by the micro-trust server, and we can send the message to the micro-server.

After the installation of Nodejs in the public network server, we also need to install some Nodejs modules, such as: Express,node-xml,jssha modules. Can be installed through the NPM command.

We use Nodejs to realize the sending and receiving of message to the micro-server, and the signature authentication with the micro-trust server.

We have installed the NODEJS environment for our students in the editing environment on the right. We will be in the next content for the students to implement the micro-trust Server signature certification.

Create the Express framework

We have already installed the Express module in the previous course and have created a file named App.js in our right environment. Now we're done with the Express framework in this file. The following code:

var express = require ("Express");
var path=require (' path ');
var app = Express ();
Server = require (' http '). Server (app);
App.set (' views ', __dirname); Set View 
app.set (' View engine ', ' html '); 
App.engine ('. html ', require (' Ejs '). __express);
Require ('./index ') (app); Routing configuration file
Server.listen (80,function () {
console.log (' App start,port ');
});

Then add a file named Test.html. Write the following content

<! DOCTYPE html>
 
 

We also want to add a file called Index.js to implement our route. Click the Add File button in the editing environment, add the file, and then we write the following code where the GET request verifies the URL legality of the configuration and the post request is used to process the micro-mail message.

Module.exports = function (APP) {
app.get ('/', function (req,res) {
res.render (' test ', {issuccess: ' Success '} )
});
App.get ('/interface ', function (req,res) {});
App.post ('/interface ', function (req,res) {});
}

So the express framework we need is complete, and of course we can add public folders and the middleware we want to use. Save the file, click "Submit to Run", and then click "Access Test" to try it. Note the address of the access test that we will use in the next section.

Micro-Trust Server configuration

We log on to the micro-credit public platform, locate the basic configuration under Developer mode, and then modify the server configuration. As shown in the figure:

First URL to fill in the public online we install Nodejs to receive and send the data path. We can fill in the "Access test" address in the previous section, and then add the corresponding route.

Token to be consistent with our custom server-side token. After completing, you can click Submit, before submitting, we start App.js (click "Submit to Run"). This will verify that the signature is valid based on our route matching.

When the configuration is complete, be sure to enable the configuration.

URL Access

Once the public platform user submits the information, the micro-server sends a GET request to the completed URL with four parameters:

Parameter Description
Signature micro-Letter encryption Signature
Timestamp time stamp
Nonce Random Number
Echostr Random string

The developer verifies the request by checking the signature (there is a check method below). If you confirm that this GET request comes from a micro-trust server, return the ECHOSTR parameter content as is, the access is valid, or the access fails.

Signature combines the token parameters and timestamp parameters and Nonce parameters that are filled in by the developer.

Encryption/validation Process:

1, the token, timestamp, nonce three parameters are sorted in dictionary order;
2. Concatenation of three parameter strings into a string for SHA1 encryption;
3, the developer obtains the encrypted string can compare with the signature, the identification this request originates from the micro-letter.
parameter Sorting

First we confirm that the request originated from the micro-server's GET request, so we can add code to the Index.js file. It is then added in the function of App.get ('/interface ', function (req,res) {}).

To get the values of each parameter, the following code:

var token= "Weixin";
var signature = req.query.signature;
var timestamp = Req.query.timestamp;
var echostr = req.query.echostr;
var nonce = req.query.nonce;

Here we set up token to match the token set in the micro-trust server.

Then the token, timestamp, nonce are sorted, the following code:

var oriarray = new Array ();
Oriarray[0] = nonce;
ORIARRAY[1] = timestamp;
ORIARRAY[2] = token;
Oriarray.sort ();

So we're done with the sort.

Parameter encryption

We've sorted the parameters in the previous section, and then we're going to make a string of the parameters in this section to SH-1 encryption. The Jssha module is used before encryption and is referenced in our files.

var Jssha = require (' Jssha ');

In the last lesson we've sorted the parameters and stored them in an array, and we can generate a string by using the Join method, which is the following code:

var original = Oriarray.join (');

Finally, the data is encrypted, as the following code:

var Jssha = require (' Jssha ');
var shaobj = new Jssha (original, ' TEXT ');
var scyptostring=shaobj.gethash (' SHA-1 ', ' HEX '); 

All right, so it generates the signature string scyptostring we need.

Signature comparison

We've got the signature string scyptostring we want, and then we can compare it to the signature from the micro-server, and then we can receive and send the message.

 if (signature = = scyptostring) {
 //authentication succeeded
 } else {
 //validation failed
 }

This article has been organized into the "JavaScript micro-Credit Development Skills Summary", welcome to learn to read.

For everyone to recommend now more attention than the micro-letter Program Tutorial: "Micro-letter Small Program Development tutorial" Small series for everyone carefully organized, hope like.

The above is the entire content of this article, I hope to help you learn

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: info-contact@alibabacloud.com 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.