后台nodejs+express从sql server中获取数据

框架怎么搭建,看下面即可:

vscode+nodejs+express 搭建一个简单网站

要在 Node.js + Express 中从数据库获取数据,需要借助数据库驱动(比如 mssql),下面是完整步骤:

一、准备工作

  1. 安装依赖:
cpp 复制代码
npm install express mssql  # mssql是SQL Server的Node.js驱动

二、完整代码示例(连接 SQL Server 并查询数据)

cpp 复制代码
// 2. 定义接口:从数据库查询数据
app.get('/get-data-bjqinfo', async (req, res) => {
    let pool;
    try {
        // 连接数据库(创建连接池)
        pool = await sql.connect(dbConfig);
        // 执行SQL查询(查询你截图里的表,假设表名是`你的表名`,请替换成实际表名)
        const result = await pool.request().query('SELECT * FROM bjqinfo');

        // 返回查询结果
        res.json({
            code: 0,
            msg: '查询成功',
            data: result.recordset  // recordset是查询到的数据集
        });
    } catch (error) {
        console.error('数据库操作失败:', error);
        res.status(500).json({
            code: -1,
            msg: '服务器异常',
            error: error.message
        });
    } finally {
        // 关闭连接池(可选,连接池会自动管理,但手动关闭更安全)
        if (pool) {
            await pool.close();
        }
    }
});

运行一下看结果:

看,查到了数据库里面的数据。

相关推荐
星光开发者4 天前
基于springboot智慧草场牛羊管理系统-计算机毕设【课程设计】50360
vue.js·spring boot·后端·mysql·php·课程设计·express
vx_Biye_Design8 天前
django房屋租赁推荐系统78455-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·课程设计·express
vx_Biye_Design11 天前
PHP中医养生馆预约系统60841-计算机课程设计、毕业设计
vue.js·spring boot·后端·mysql·php·课程设计·express
夏幻灵12 天前
Express 静态资源托管详解:从本地 HTML 到 express.static()
前端·html·express
毕业设计70315 天前
(免费领源码)基于node的学生公寓管理系统06412- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
vue.js·mysql·node.js·maven·mybatis·express·b/s
Wx-bishekaifayuan16 天前
springboot桂味美食分享系统40540-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
Wx-bishekaifayuan18 天前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
人工智能·spring boot·后端·python·课程设计·express·旅游
Wx-bishekaifayuan18 天前
springboot汉中旅游服务平台41442-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
Wx-bishekaifayuan18 天前
springboot公考微信小程序35511-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
Wx-bishekaifayuan18 天前
springboot线上中医药养生平台管理系统36753-计算机课程设计、毕业设计
java·spring boot·后端·spring·课程设计·express·旅游