node+mySQL接口

数据库



Navicat for mysql 建表写入数据

node.js

app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const express = require('express');
const mysql = require('mysql');
const app = express();

//建立连接
const pool = mysql.createPool({
host:'localhost',
user:'root',
password:'123456',
database:'dataBaseName',
port:3306
})

// 设置为可跨域
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});

// 前端请求地址为 http://localhost:8001/login
app.get('/login',(req,res)=>{
pool.query(`SELECT * FROM login`, (qerr,vals,fields)=>{
let rows = JSON.stringify(vals);
res.send(rows)
})
})

app.listen(8001,() => {
console.log('ok');
})

-------------本文结束感谢您的阅读-------------