This article mainly shares with you the use of node. js public platform development tutorial, how to develop, interested friends can refer to this article mainly to share with you the use of node. js development public platform tutorial, how to develop, interested friends can refer
How can nodejs be used to develop public platforms?
Let's not talk about anything more. First, let's briefly introduce the basic principles of the public platform.
The server is equivalent to a forwarding server. a terminal (such as a mobile phone or tablet) initiates a request to the server and forwards the request to the custom service (here is our specific implementation ). After the service is processed, the server forwards the response to the terminal. the communication protocol is HTTP and the data format is XML.
The specific process is shown in:
Var express = require ("express"); var path = require ('PATH'); var app = express (); server = require ('http '). server (app); app. set ('View', dirname); // sets the View app. set ('View engine ', 'html'); app. engine ('.html ', require ('ejs '). express); require ('. /index') (app); // route configuration file server. listen (80, function () {console. log ('app start, port 80. ');});
Then upload another file named test.html. Write the following content
Huizhi network<% = Issuccess %>
We also need to add a file named index. js to implement our routing. Click the Add file button in the editing environment to add the file. then we write the following code. the GET request is used to verify the validity of the configured URL and the POST request is used to process the 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){});}
This completes the express framework we need. of course, we can also add public folders and middleware we will use. Save the file, click submit and run, and then click access test to try it. Write down the access test address, which will be used in the next section.
Server Configuration
Log on to the public platform, find the basic configuration in developer mode, and modify the server configuration. :
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 the token to make it consistent with the token set on the server.
Sort the tokens, timestamp, and nonce in the list, as shown in the following code:
var oriArray = new Array();oriArray[0] = nonce;oriArray[1] = timestamp;oriArray[2] = token;oriArray.sort();
This completes the sorting.
Parameter encryption
In the preceding section, we have sorted the parameters. in this section, we need to combine the parameters into a string for SH-1 encryption. Jssha module is used before encryption. we need to reference this module in our files.
var jsSHA = require('jssha');
In the previous lesson, we sorted the parameters and placed them in arrays. we can use the join method to generate a string, as shown in the following code:
var original = oriArray.join('');
The following code encrypts the data:
var jsSHA = require('jssha');var shaObj = new jsSHA(original, 'TEXT');var scyptoString=shaObj.getHash('SHA-1', 'HEX');
Then the required signature string scyptoString is generated.
Signature comparison
We have obtained the desired signature string scyptoString, and then we can compare it with the signature from the server. after comparison, we can receive and send messages.
If (signature = scyptoString) {// verification successful} else {// verification failed}
The above is the details of the public platform development tutorial using node. js. For more information, see other related articles on php Chinese network!