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

      <tfoot id='k2RrC'></tfoot>
      <legend id='k2RrC'><style id='k2RrC'><dir id='k2RrC'><q id='k2RrC'></q></dir></style></legend>
        <bdo id='k2RrC'></bdo><ul id='k2RrC'></ul>

        <i id='k2RrC'><tr id='k2RrC'><dt id='k2RrC'><q id='k2RrC'><span id='k2RrC'><b id='k2RrC'><form id='k2RrC'><ins id='k2RrC'></ins><ul id='k2RrC'></ul><sub id='k2RrC'></sub></form><legend id='k2RrC'></legend><bdo id='k2RrC'><pre id='k2RrC'><center id='k2RrC'></center></pre></bdo></b><th id='k2RrC'></th></span></q></dt></tr></i><div id='k2RrC'><tfoot id='k2RrC'></tfoot><dl id='k2RrC'><fieldset id='k2RrC'></fieldset></dl></div>
      1. 如何在 nodejs 中的单个文件中提供 mysql 数据库连接

        How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
          <i id='ZcOu6'><tr id='ZcOu6'><dt id='ZcOu6'><q id='ZcOu6'><span id='ZcOu6'><b id='ZcOu6'><form id='ZcOu6'><ins id='ZcOu6'></ins><ul id='ZcOu6'></ul><sub id='ZcOu6'></sub></form><legend id='ZcOu6'></legend><bdo id='ZcOu6'><pre id='ZcOu6'><center id='ZcOu6'></center></pre></bdo></b><th id='ZcOu6'></th></span></q></dt></tr></i><div id='ZcOu6'><tfoot id='ZcOu6'></tfoot><dl id='ZcOu6'><fieldset id='ZcOu6'></fieldset></dl></div>
          <legend id='ZcOu6'><style id='ZcOu6'><dir id='ZcOu6'><q id='ZcOu6'></q></dir></style></legend>

          • <small id='ZcOu6'></small><noframes id='ZcOu6'>

              <tfoot id='ZcOu6'></tfoot>
                  <tbody id='ZcOu6'></tbody>
                  <bdo id='ZcOu6'></bdo><ul id='ZcOu6'></ul>
                • 本文介绍了如何在 nodejs 中的单个文件中提供 mysql 数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要为模块提供 mysql 连接.我有一个这样的代码.

                  var express = require('express'),app = express(),server = require('http').createServer(app);var mysql = require('mysql');var 连接 = mysql.createConnection({主机:'127.0.0.1',用户:'root',密码 : '',数据库:'聊天'});connection.connect(function(err) {如果(错误){console.error('错误连接:' + err.stack);返回;}});app.get('/save', function(req,res){var post = {from:'me', to:'you', msg:'hi'};var query = connection.query('INSERT INTO messages SET ?', post, function(err, result) {如果(错误)抛出错误;});});服务器.听(3000);

                  但是我们如何为所有模块提供一次 mysql 连接.

                  解决方案

                  您可以创建一个 db 包装器然后需要它.node 的 require 每次都返回相同的模块实例,因此您可以执行连接并返回处理程序.来自 Node.js 文档:

                  <块引用>

                  对 require('foo') 的每次调用都将返回完全相同的对象,如果它解析为相同的文件.

                  你可以创建db.js:

                  var mysql = require('mysql');var 连接 = mysql.createConnection({主机:'127.0.0.1',用户:'root',密码 : '',数据库:'聊天'});connection.connect(function(err) {如果(错误)抛出错误;});module.exports = 连接;

                  然后在您的 app.js 中,您只需需要它.

                  var express = require('express');var app = express();var db = require('./db');app.get('/save',function(req,res){var post = {from:'me', to:'you', msg:'hi'};db.query('INSERT INTO messages SET ?', post, function(err, result) {如果(错误)抛出错误;});});服务器.听(3000);

                  这种方法允许您抽象任何连接细节,在整个应用程序中包装您想要公开和 require db 的任何其他内容,同时由于 node require 的工作方式而保持与数据库的一个连接:)

                  I need to provide the mysql connection for modules. I have a code like this.

                  var express = require('express'),
                  app = express(),
                  server = require('http').createServer(app);
                  
                  var mysql      = require('mysql');
                  var connection = mysql.createConnection({
                      host     : '127.0.0.1',
                      user     : 'root',
                      password : '',
                      database    : 'chat'
                  });
                  
                  connection.connect(function(err) {
                      if (err) {
                          console.error('error connecting: ' + err.stack);
                          return;
                      }
                  });
                  
                  app.get('/save', function(req,res){
                      var post  = {from:'me', to:'you', msg:'hi'};
                      var query = connection.query('INSERT INTO messages SET ?', post, function(err, result) {
                          if (err) throw err;
                      });
                  });
                  
                  server.listen(3000);
                  

                  But how we provide one time mysql connection for all the modules.

                  解决方案

                  You could create a db wrapper then require it. node's require returns the same instance of a module every time, so you can perform your connection and return a handler. From the Node.js docs:

                  every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

                  You could create db.js:

                  var mysql = require('mysql');
                  var connection = mysql.createConnection({
                      host     : '127.0.0.1',
                      user     : 'root',
                      password : '',
                      database : 'chat'
                  });
                  
                  connection.connect(function(err) {
                      if (err) throw err;
                  });
                  
                  module.exports = connection;
                  

                  Then in your app.js, you would simply require it.

                  var express = require('express');
                  var app = express();
                  var db = require('./db');
                  
                  app.get('/save',function(req,res){
                      var post  = {from:'me', to:'you', msg:'hi'};
                      db.query('INSERT INTO messages SET ?', post, function(err, result) {
                        if (err) throw err;
                      });
                  });
                  
                  server.listen(3000);
                  

                  This approach allows you to abstract any connection details, wrap anything else you want to expose and require db throughout your application while maintaining one connection to your db thanks to how node require works :)

                  这篇关于如何在 nodejs 中的单个文件中提供 mysql 数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                  How can I use a single mssql connection pool across several routes in an Express 4 web application?(如何在 Express 4 Web 应用程序中跨多个路由使用单个 mssql 连接池?)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  How to use sqlite3 module with electron?(如何在电子中使用 sqlite3 模块?)
                  SQL Server Express vs MS Access(SQL Server Express 与 MS Access)
                  Writing an SQL query to SELECT item from the following table(编写 SQL 查询以从下表中选择项目)
                    <bdo id='uP1jI'></bdo><ul id='uP1jI'></ul>
                    • <i id='uP1jI'><tr id='uP1jI'><dt id='uP1jI'><q id='uP1jI'><span id='uP1jI'><b id='uP1jI'><form id='uP1jI'><ins id='uP1jI'></ins><ul id='uP1jI'></ul><sub id='uP1jI'></sub></form><legend id='uP1jI'></legend><bdo id='uP1jI'><pre id='uP1jI'><center id='uP1jI'></center></pre></bdo></b><th id='uP1jI'></th></span></q></dt></tr></i><div id='uP1jI'><tfoot id='uP1jI'></tfoot><dl id='uP1jI'><fieldset id='uP1jI'></fieldset></dl></div>
                      <tfoot id='uP1jI'></tfoot>
                        <tbody id='uP1jI'></tbody>

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

                        1. <legend id='uP1jI'><style id='uP1jI'><dir id='uP1jI'><q id='uP1jI'></q></dir></style></legend>