Novice beginner node.js for development, in the application for access to the public platform development test number, because the official samples only for PHP and java and other ways of code, and node beginners do not understand, when submitted to the interface test URL, often unsuccessful, indicating "configuration failure", here is the application of the test number, interface configuration information node.js code implementation. The process of applying for domain name, parsing, opening host, and applying for SSL certificate is described in detail in the online tutorial development document, not in detail.
1,Click on the application test number. Get appID and appsecret information and fill in the server configuration
2. Write the node.js validation code as follows. The package files such as koa sha1 quoted are installed in advance of npm install.
npm install koa koa-sslify sha1 fs http https
'use strict' /* Access verification of public address Node.js Koa + SSL test interface The WeChat server will send the GET request portability parameter as shown in the table below. ================================== token: Your TOKEN signature: Encrypting signature to verify content, sha1([token,timestamp,nonce].sort()) nonce: random number timestamp: time stamp ecostr: Random string sha: Your verification signature, the calculation results need to be consistent with signature */ const Koa = require('koa') const http = require("http") const https = require("https") const fs = require("fs") const sslify = require("koa-sslify").default; const sha1 = require('sha1') const port = 80 const SSLport = 443 // The content of config is the ID authentication information of WeChat test number page that you apply for. let config = { wechat: { appID: 'wx88888888888888', appSecret: 'xxxxxxxxxxxxxxxxxxxxxxx', token: 'xxxxxx' } } const options = { key: fs.readFileSync("./ssl/privkey.pem"), //Asynchronous reading of private key files cert: fs.readFileSync("./ssl/fullchain.pem"), //Asynchronous Reading of Certificate Files requestCert: false, //Is Client Certificate Requested rejectUnauthorized: false //Whether to reject client connection requests for certificates issued by untrusted CA s } let app = new Koa() app.use(sslify()); app.use(function *(next) { let token = config.wechat.token let signature = this.query.signature let nonce = this.query.nonce let timestamp = this.query.timestamp let echostr = this.query.echostr let str = [token, timestamp, nonce].sort().join('') let sha = sha1(str) if (sha === signature) { this.body = echostr + '' } else { this.body = 'wrong' } }) http.createServer(app.callback()).listen(port) https.createServer(options,app.callback()).listen(SSLport) console.log('Wechat Server Start......')
3, submit configuration information, verify that the message does come from your server.
Related instructions, you can refer to the development document. Access Guide.