node中express链接数据库(Mysql)

一、创建配置文件

(1)安装mysql

npm i mysql -s

在package中显示MySQL的版本号

(2)创建一个util的文件夹装配置文件config.js

//导入MySQL包
const mysql = require("mysql")
//链接数据库的配置信息
const config = {
    host:"localhost",
    port:"3306",
    user:"root",
    password:"root123",
    database:"user_db"
}
//暴露接口
module.exports ={
    opreateDB: function (sql,sqlArr){
        return new Promise((resolve,reject) => {
            //连接数据库
            let pool = mysql.createPool(config);
            pool.getConnection((err,conn) => {
                if(err) reject(err);
                conn.query(sql,sqlArr,(err,data) =>{
                    if (err) reject(err);
                    else{
                        resolve(data);
                    }
                    //销毁链接
                    conn.destroy();
                })
            })
        }).catch(e =>{
            //捕捉错误信息
            console.log(e);
        })
    }
}

(3)创建一个controller来控制数据库的链接


//导入config文件
var db = require("../util/dbconfig")
//async实现同步处理
async function login(req,res){
    //let里面的参数名要和外面传进来的参数名一致
    let{username,pwd} = req.query;
    let sql ="select * from user_tbl where user_name=? and user_pwd=?";
//await等待代码执行完再执行下一行代码,防止异步
    let data =await db.opreateDB(sql,[username,pwd])
    
    res.send(data);
}

module.exports = {
    login
}

 效果图

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
To connect to a MySQL database from an Express.js app, you will need to use a MySQL driver for Node.js. One of the most popular and well-maintained drivers is `mysql2`. Here is an example of how you can use `mysql2` to connect to a MySQL database from an Express.js app: 1. Install the `mysql2` package: ```bash npm install mysql2 ``` 2. Require the `mysql2` package in your Express.js app: ```javascript const mysql = require('mysql2'); ``` 3. Create a connection to the database using the `createConnection` method: ```javascript const connection = mysql.createConnection({ host: 'localhost', user: 'user', password: 'password', database: 'database' }); ``` 4. Connect to the database by calling the `connect` method: ```javascript connection.connect(function(err) { if (err) { console.error('Error connecting to the database: ' + err.stack); return; } console.log('Connected to the database as id ' + connection.threadId); }); ``` 5. Use the `query` method to execute SQL statements: ```javascript connection.query('SELECT * FROM table', function(err, rows, fields) { if (err) { console.error(err); return; } console.log(rows); }); ``` 6. When you are finished with the connection, end it by calling the `end` method: ```javascript connection.end(); ``` It's important to note that the `mysql2` driver uses connection pooling, which means that it will reuse connections rather than creating a new connection for each request. This can help improve the performance of your app by reducing the overhead of establishing new connections. I hope this helps! Let me know if you have any questions.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浓父小杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值