The first step is to download Express

The Express creation server was introduced

npm install express -SCopy the code

const express = require('express')const app = express()app.listen(5000, ()=>{console.log()'http://127.0.0.1:5000')})  Copy the code

 

Step 2 Connect to the database

You need to download mysql to connect to the database

- npm install mysql -S -Copy the code

Body needs to parse the form data, so body-Parser needs to be added

// create database connection const mysql = require('mysql')
const conn = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:' ',
    database:'test'}) // bodyParser const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended:false}))Copy the code

Now we’re going to do interfaces

Get the data first

// Get all data app.get('/api/getheros',(req,res) => {// Define SQL statement const sqlStr ='select * from text where isdel=0'
    conn.query(sqlStr,(err,results) => {
        console.log(results)
        if(err) return res.json({err_code:1,message:'Fetch failed',affectedRows:0})
        res.json({
            err_code:0,message:results,affectedRows:0
        })
    })Copy the code

The original address: blog.csdn.net/xl4277/arti…