node.js链接mysql做api

/* 引入express框架 */
const express = require('express');
const app = express();

/* 引入cors */
const cors = require('cors');
app.use(cors());

/* 引入body-parser */
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
      extended: false }));
/* 引入mysql */
const mysql = require('mysql');
const conn = mysql.createConnection({
     
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'db_user',
})
conn.connect();

/* 监听端口 */
app.listen(8080, () => {
     
    console.log('——————————服务已启动——————————');
})

app.get('/', (req, res) => {
     
    res.send('

服务已启动

'
); console.log(req.url) }) app.get('/api/getUserList', (req, res) => { const sqlStr = 'SELECT * FROM td_userinfo' conn.query(sqlStr, (error, results) => { res.json({ code: 10000, message: results}) }) console.log(req.query) }) app.post('/api/getUserList', (req, res) => { const sqlStr = 'SELECT * FROM td_userinfo' conn.query(sqlStr, (error, results) => { /if (error) return res.json({ code: 10001, message: error}) res.json({ code: 10000, message: results}) console.log(results) console.log(req.url) }) console.log(req.body) })

你可能感兴趣的:(node.js,api)