Home  >  Article  >  WeChat Applet  >  Share a tutorial on setting up a WeChat applet to access the nodejs interface server

Share a tutorial on setting up a WeChat applet to access the nodejs interface server

Y2J
Y2JOriginal
2017-04-27 15:08:105792browse

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.

Share a tutorial on setting up a WeChat applet to access the nodejs interface server

Symantec SSL Certificate Application

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

Share a tutorial on setting up a WeChat applet to access the nodejs interface server


The directory after decompression is as shown below. The files we need to use are The first two files are our private key files.

Share a tutorial on setting up a WeChat applet to access the nodejs interface server


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.

Share a tutorial on setting up a WeChat applet to access the nodejs interface server

Printing interface data in real machine debugging mode

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn