Node.js 如何关闭节点js mssql中的连接?

Node.js 如何关闭节点js mssql中的连接?,node.js,sql-server,rest,Node.js,Sql Server,Rest,我正在使用mssqlnpm连接到sqlserver。当我连接到数据库并获取或发布数据时,连接尚未打开。每次请求后如何关闭连接 这是我使用的一种请求: router.get('/getnotification/:userid',async(req,resp,next)=>{ (async function(){ let pool = null; try{ pool = await connection l

我正在使用
mssql
npm连接到sqlserver。当我连接到数据库并获取或发布数据时,连接尚未打开。每次请求后如何关闭连接

这是我使用的一种请求:

router.get('/getnotification/:userid',async(req,resp,next)=>{
    (async function(){
        let pool = null;
        try{
            pool = await connection
            let result = await pool.request()
            .input('input_id',sql.BigInt,req.params.userid)
            .query(`exec showNotification 0,@input_id`)

            if(result){
                resp.status(200).send(result.recordset).end;
            }
            //  pool.close()
        }catch(err){
                resp.status(500).send(err).end;
            //  pool.close()  
        }

    })()
})

我也在请求完成后使用
pool.close()
,但对于下一个请求,连接被关闭。

您可以创建一个用于关闭池的中间件函数,并在绑定
pool
以请求obj后使用它

const closePoolMiddleware = function (req, res, next) {

  if(!req.pool) {
    next("pass error to express")
  }
  req.pool.close()
  next()
})
然后呢,

router.get('/getnotification/:userid',async(req,resp,next)=>{
    (async function(){
        let pool = null;
        try{
            pool = await connection
            let result = await pool.request()
            .input('input_id',sql.BigInt,req.params.userid)
            .query(`exec showNotification 0,@input_id`)

            if(result){
                resp.status(200).send(result.recordset).end;
            }
            //  pool.close()
        }catch(err){
                resp.status(500).send(err).end;
            //  pool.close()  
        }

    })()
}, closePoolMiddleware)

为每个请求或请求重新打开连接