The applet uses Nodejs as the server, and Nodejs is connected to the MYSQL database.
1. Build environment
- Create a new empty folder: Win + R to enter the cmd command interface and execute it
npm install express body-parser request
2. Configure Nodejs
- Create a new index.js file in the directory and configure the following code:
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
const PORT = 5008
app.use(bodyParser.json())
app.get('/',(req,res)=>{
res.send('Server is running!')
})
app.listen(PORT,()=>{
console.log(`Server is running on localhost:${
PORT}`);
})
- Start Nodejs: Enter in the terminal
node index.js
3. Interacting with mini programs
- Server-side code implementation: still in the index.js file
const APP_ID = "wx4f9ef75353fd5bc3";
const APP_SECRET = "d9317db76db37df632d729ca0bdf1f2a";
app.get("access_token", (req, res) => {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${
APP_ID}&secret=${
APP_SECRET}`;
request.get(url, (error, response, body) => {
if (!error && response.statusCode === 200) {