Recently I am developing a WeChat application account applet. The background data interface of the applet requires https secure request, so my nodejs server needs to be able to provide https support. Now I will talk about the entire https server building process.
First of all, I tried the previous expired certificate. When simulating in the developer tools, I can access the interface normally. When testing on the mobile phone, the wx.request of the WeChat applet will report an SSL handshake failure. error (request error : request: fail ssl hand shake error), so you can only reapply for a certificate. It is recommended to use Alibaba Cloud's Symantec SSL/TLS certificate, which is supported by WeChat and can be applied for free for one year.
After the application is completed, the review period is usually 1 to 3 days, and you can download the certificate file. We Select other categories of certificates
The directory after decompression is as shown below. The files we need to use are The first two files are our private key files.
Then we need to build our https server. Here we use the https module that comes with nodejs
var https = require('https') ,fs = require("fs"); var express = require('express'); var app = express(); var options = { key: fs.readFileSync('./213988289600767.key'), cert: fs.readFileSync('./213988289600767.pem') }; https.createServer(options, app).listen(8081, function () { console.log('Https server listening on port ' + 8081); });
It should be noted at this time that WeChat’s mini program only supports domain name interfaces without ports, and does not support IP addresses and interfaces, so we need to map to port 80 and bind the registered domain name in order to be Accessed by WeChat applet.
Finally we can access our nodejs server interface through the wx.request method.
The above is the detailed content of Share a tutorial on setting up a WeChat applet to access the nodejs interface server. For more information, please follow other related articles on the PHP Chinese website!