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 itnpm install express body-parser request
    Insert image description here

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 terminalnode index.js
    Insert image description here

3. Interacting with mini programs

  • Server-side code implementation: still in the index.js file
// 小程序设置
const APP_ID = "wx4f9ef75353fd5bc3";
const APP_SECRET = "d9317db76db37df632d729ca0bdf1f2a";

// 获取access_token
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) {
   
    
    
    

Guess you like

Origin blog.csdn.net/m0_50298323/article/details/134581675