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 = {/*...*
                                
                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Building a comma separated list?(建立一个逗号分隔的列表?)
                Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                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>

                <tfoot id='v0FWa'></tfoot>

                <small id='v0FWa'></small><noframes id='v0FWa'>

                1. <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>

                            <tbody id='v0FWa'></tbody>