1. <small id='V8Gll'></small><noframes id='V8Gll'>

      <tfoot id='V8Gll'></tfoot>
    2. <i id='V8Gll'><tr id='V8Gll'><dt id='V8Gll'><q id='V8Gll'><span id='V8Gll'><b id='V8Gll'><form id='V8Gll'><ins id='V8Gll'></ins><ul id='V8Gll'></ul><sub id='V8Gll'></sub></form><legend id='V8Gll'></legend><bdo id='V8Gll'><pre id='V8Gll'><center id='V8Gll'></center></pre></bdo></b><th id='V8Gll'></th></span></q></dt></tr></i><div id='V8Gll'><tfoot id='V8Gll'></tfoot><dl id='V8Gll'><fieldset id='V8Gll'></fieldset></dl></div>
        <bdo id='V8Gll'></bdo><ul id='V8Gll'></ul>
      <legend id='V8Gll'><style id='V8Gll'><dir id='V8Gll'><q id='V8Gll'></q></dir></style></legend>

      如何在 Express 4 Web 应用程序中跨多个路由使用单个 mssql 连接池?

      How can I use a single mssql connection pool across several routes in an Express 4 web application?(如何在 Express 4 Web 应用程序中跨多个路由使用单个 mssql 连接池?)

        <i id='E697r'><tr id='E697r'><dt id='E697r'><q id='E697r'><span id='E697r'><b id='E697r'><form id='E697r'><ins id='E697r'></ins><ul id='E697r'></ul><sub id='E697r'></sub></form><legend id='E697r'></legend><bdo id='E697r'><pre id='E697r'><center id='E697r'></center></pre></bdo></b><th id='E697r'></th></span></q></dt></tr></i><div id='E697r'><tfoot id='E697r'></tfoot><dl id='E697r'><fieldset id='E697r'></fieldset></dl></div>

          1. <tfoot id='E697r'></tfoot>
            • <small id='E697r'></small><noframes id='E697r'>

              • <bdo id='E697r'></bdo><ul id='E697r'></ul>
                <legend id='E697r'><style id='E697r'><dir id='E697r'><q id='E697r'></q></dir></style></legend>
                  <tbody id='E697r'></tbody>
                本文介绍了如何在 Express 4 Web 应用程序中跨多个路由使用单个 mssql 连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想在 Node JS Express 4 中使用 node-mssql 作为 MSSQL 数据库连接器Web应用程序.路由处理程序逻辑在单独的文件中处理.

                I want to use node-mssql as a MSSQL database connector in a Node JS Express 4 web application. Route handler logic is handled in separate files.

                如何创建单个/全局连接池并在处理路由逻辑的多个文件中使用它?我不想在每个路由处理函数/文件中创建一个新的连接池.

                How do I create a single/global connection pool and use it across several files where route logic is handled? I don't want to make a new connection pool in each route handler function/file.

                推荐答案

                自从我问和回答这个问题已经 3 年了.从那时起,一些事情发生了变化.这是我今天推荐的基于 ES6、mssql 4 和 Express 4 的新解决方案.

                It's been 3 years since I asked and answered the question. Since then a few things have changed. Here's the new solution based on ES6, mssql 4 and Express 4 that I would suggest today.

                这里有两个关键元素在起作用.

                Two key elements are at play here.

                1. 模块在第一次加载后被缓存.这意味着每次调用 require('./db') 都将返回完全相同的对象.db.js 的第一个 require 将运行该文件并创建承诺并将其导出.db.js 的第二个要求将在不运行文件的情况下返回相同的承诺.正是这个承诺将与游泳池一起解决.
                2. 可以再次验证承诺.如果之前解决了,它会立即再次解决第一次解决的问题,也就是池.
                1. Modules are cached after the first time they are loaded. This means that every call to require('./db') will return exactly the same object. The first require of db.js will run that file and create the promise and export it. The second require of db.js will return THAT same promise without running the file. And it's that promise that will resolve with the pool.
                2. A promise can be thenified again. And if it resolved before, it will immediately resolve again with whatever it resolved with the first time, which is the pool.

                server.js

                const express = require('express')
                // require route handlers.
                // they will all include the same connection pool
                const set1Router = require('./routes/set1')
                const set2Router = require('./routes/set2')
                
                // generic express stuff
                const app = express()
                
                // ...
                app.use('/set1', set1Router)
                app.use('/set2', set2Router)
                
                // No need to connect the pool
                // Just start the web server
                
                const server = app.listen(process.env.PORT || 3000, () => {
                  const host = server.address().address
                  const port = server.address().port
                
                  console.log(`Example app listening at http://${host}:${port}`)
                })
                

                db.js

                const sql = require('mssql')
                const config = {/*...*
                                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
                                
                上一篇:在 SQL Server 中定义一对一关系 下一篇:如何在 nodejs 中的单个文件中提供 mysql 数据库连接
                相关文档推荐 建立一个逗号分隔的列表? Building a comma separated list?(建立一个逗号分隔的列表?) 尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误 Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误) 如何将 Excel 文件导入 SQL Server? How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?) 使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件 Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件) 将字段值连接到 SQL Server 中的字符串 Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串) SQL Server 批量插入带有不一致引号的 CSV 文件 SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                <legend id='v0FWa'><style id='v0FWa'><dir id='v0FWa'><q id='v0FWa'></q></dir></style></legend><q id='v0FWa'></q><tfoot id='v0FWa'></tfoot><small id='v0FWa'></small><noframes id='v0FWa'><small id='v0FWa'><b id='v0FWa'></b><style id='v0FWa'></style><i id='v0FWa'></i><small id='v0FWa'><dl id='v0FWa'></dl><fieldset id='v0FWa'><form id='v0FWa'><dt id='v0FWa'><code id='v0FWa'></code><code id='v0FWa'><div id='v0FWa'></div></code></dt></form></fieldset></small></small><thead id='v0FWa'><kbd id='v0FWa'></kbd><sup id='v0FWa'><th id='v0FWa'></th></sup></thead><sup id='v0FWa'><strong id='v0FWa'><i id='v0FWa'></i></strong><small id='v0FWa'><div id='v0FWa'></div></small><ins id='v0FWa'></ins></sup><legend id='v0FWa'><table id='v0FWa'></table></legend><i id='v0FWa'><tr id='v0FWa'><dt id='v0FWa'><q id='v0FWa'><span id='v0FWa'><b id='v0FWa'><form id='v0FWa'><ins id='v0FWa'></ins><ul id='v0FWa'></ul><sub id='v0FWa'></sub></form><legend id='v0FWa'></legend><bdo id='v0FWa'><pre id='v0FWa'><center id='v0FWa'></center></pre></bdo></b><th id='v0FWa'></th></span></q></dt></tr></i><div id='v0FWa'><tfoot id='v0FWa'></tfoot><dl id='v0FWa'><fieldset id='v0FWa'></fieldset></dl></div><bdo id='v0FWa'></bdo><ul id='v0FWa'></ul><strong id='v0FWa'><tr id='v0FWa'></tr></strong><label id='v0FWa'></label><strike id='v0FWa'></strike><option id='v0FWa'><u id='v0FWa'><ol id='v0FWa'><blockquote id='v0FWa'></blockquote></ol></u></option><table id='v0FWa'></table><tbody id='v0FWa'></tbody> 栏目导航 前端开发问题Java开发问题C/C++开发问题Python开发问题C#/.NET开发问题php开发问题移动开发问题数据库问题 最新文章 • MySQL:将逗号分隔的列表拆分为多行... • 在 SQL Server 中将 Varchar 转换为... • SQL 链接服务器查询非常非常慢... • 在 SQL Server 中获取工作日... • SQL Server BEFORE UPDATE 触发器,... • SQL Server“FOR XML"连接两个... • E:从ubuntu 20.04完全删除mysql时,... • SQL 在两表排列中向上或向下移动行... • 如何在 SQL Server 2016 中创建宽表?... • 并非所有参数都在 SQL 语句中使用(Py... • SQL Server 相当于 MySQL 中的 subst... • 更改“Mysql Row size too large&quo... 热门文章 • MySQL:将逗号分隔的列表拆分为多行... • 在 SQL Server 中将 Varchar 转换为... • SQL 链接服务器查询非常非常慢... • 在 SQL Server 中获取工作日... • SQL Server BEFORE UPDATE 触发器,... • SQL Server“FOR XML"连接两个... • E:从ubuntu 20.04完全删除mysql时,... • SQL 在两表排列中向上或向下移动行... • 如何在 SQL Server 2016 中创建宽表?... • 并非所有参数都在 SQL 语句中使用(Py... • SQL Server 相当于 MySQL 中的 subst... • 更改“Mysql Row size too large&quo... 热门标签 五金机械 教育培训 机械设备 环保公司 新闻资讯 服装服饰 营销型 轴承 电子元件 零部件 电子科技 电子产品 环保科技 培训机构 电子商城 双语 中英双语 织梦模板 dede 外语学校 竞价网站源码 竞价培训网 门户网站 织梦笑话网 dedecms笑话网 织梦源码 网站建设 搞笑图片 织梦教程 旅游网站源码 织梦旅游网 学校培训 html5 企业织梦源码 医院源码 后台样式 移动营销页 chatgpt 整形医院 大学医院 新手建站 客服代码 洗衣机维修 企业网站 淘宝客 导航菜单 教育网站 学校源码 装修网站 装修模板 美容整形 女性健康 妈妈网 机械源码 建站公司 珠宝首饰 苹果网站 手机资讯 管理平台 织梦模版打包 妇科源码 安卓市场源码 男性时尚网 健康之家 app应用网站 笑话网站 下载站 车辆管理系统 中医院网站 家装网站源码
                网站首页 - 免责声明- 最新公告- 充值相关 - 网站地图 Copyright © 2022-2023 深圳市沃梦达电子商务有限公司 All Rights Reserved. 粤ICP备14083021号