preface

Have seen some node to do wechat public account authentication tutorial, I also want to use node to wechat and small program development content to learn, including login mechanism, after the total reliance on the back end of friends to help, expect to complete the full stack development.

As a result, I just completed the first step of domain name authentication. I recorded the process and learned together.

directory

  1. Cloud server purchase
  2. Linux installation node
  3. Install Express scaffolding and dependencies
  4. Automatically restart the Nodemon plug-in
  5. Configuration of Public Account
  6. Node encryption and authentication
  7. Mining pit refers to the north
  8. conclusion

Body content

1. Cloud server purchase

This step took a long time, looking for a cheap cloud server, now I use the cloud server of Didi Cloud, 9 yuan for the first month, 20 yuan for the renewal of the next three months, take advantage of the discount, grasp the learning haha.

2. Install Node on Linux

Before a bunch of detour, because Linux is not familiar with, with the way to download and reconfigure environment variables is always wrong, learning important, did not tangle, directly changed the way.

$ sudo yum -y install nodejs 
Copy the code

3. Install Express scaffolding and dependencies

3.1 Install the Express scaffolding

$ npm install express-generator -g
Copy the code

3.2 Creating directories and Applications and Installing Dependencies

// Create source directory and go to $mkdir dir nodeApp $cd/nodeApp // Create application $express weiChart $cd/weiChart install requires the dependency package $NPM installCopy the code

4. Automatically restart the Nodemon plug-in

At this point, you can start the application directly, but consider previewing and debugging the application while modifying, which will restart the application indefinitely, so you configure the Nodemon plug-in, which automatically restarts the application after modifying the code.

4.1 installation

$NPM install -g nodemon // Go to the bin directory of the applicationcd /bin
Copy the code

4.2 Creating a Configuration File

You can create it using vim or you can create it locally and drag it into the bin directory

<! --nodemon configuration file nodemon.json --> {"restartable": "rs"."ignore": [
        ".git".".svn"."node_modules/**/node_modules"]."verbose": true."execMap": {
        "js": "node --harmony"
    },
    "watch": []."env": {
        "NODE_ENV": "development"
    },
    "ext": "js json"
}
Copy the code

Created using Vim

$vim nodemon.json // Paste the file and press Esc to enter':wq'Save and exit.Copy the code

4.3 Starting An Application Using Nodemon

// Enter the project root directory $cd ../
$ nodemon npm start
Copy the code

5. Public account configuration

Before this you should register to complete the public number, login successfully click on the left menu development -> Basic configuration.

Click Modify configuration to enter the configuration interface, enter your server address and custom token, and then randomly generate a encryption key, the default encryption method is good.

Note: I am using the /weiChart path, not the project root directory, so I need to configure it in the Express route.

6. Node encryption and authentication

6.1 Installing Dependencies

$ npm install jssha --save-dev
Copy the code

Go to the routes file in the root directory of the project and open index.js to develop.

6.2 Importing A Dependency

var jsSHA = require('jssha');
Copy the code

6.3 Creating a wechat Route


router.get('/weiChart'.function(req, res, next) {// set the mobile token to be consistent with the wechat public account var token="6d05aedab138be6bbbf40b41d65a45ec"; Obtain the parameters signature, timestamp, nonce, echostr var signature = req.query.signature,// wechat encryption signature timestamp = Req.query.timestamp,// nonce = req.query.nonce,// random echostr = req.query.echostr; Var array = [token,timestamp,nonce]; var array = [token,timestamp,nonce]; array.sort(); Var tempStr = array. Join (tempStr = array.' ');
	    var shaObj = new jsSHA('SHA-1'.'TEXT');
	    shaObj.update(tempStr);
	    var scyptoString=shaObj.getHash('HEX'); //4. The encrypted string obtained by the developer can be compared with Signature to identify the request as originating from wechatif(signature === scyptoString){
	    	console.log('Validation successful')
	        res.send(echostr);
	    }else{
	    	console.log('Validation failed')
	        res.send('Validation failed'); }});Copy the code

6.4 Binding Submission

Start the app, and then click the submit button on the wechat official account configuration page. There will be a prompt for success or failure. Wish you success.

7. Mine the pit to the north

We followed the steps of developing the wechat public platform with Node.js before, and the second and third steps in the comments were marked with two holes

  • Sorting: Wechat document is token, timestamp, nonce, the order must be correct.
  • JsSHA method: To do this correctly, new jsSHA(‘ sha-1 ‘, ‘TEXT’).

The article screenshots

  • The order is not consistent with the official website

  • The jsSHA usage is incorrect

8. To summarize

  • Look at the concept materials and tutorials

    Implementation is actually quite simple, before doing read a lot of wechat authentication data, you can find a pile of online, and then practice when the train of thought.

  • Practice look at the documentation to find the method

    Which method error on which method of the document, do not a error in another tutorial to go.

Shallow talent, if there are problems kindly oblige.