0

我正在创建一个 node.js 应用程序,它将查看传递给它的 URL,然后返回正确的数据库连接字符串。下面是我的 genericServer.js 文件。第二个代码块是我的 Connection.js 文件。当一个 id 在 URL 中传递给 genericServer 文件时,我将它传递给中间件 connection.getConnString。

var express = require('express');
var connection = require('./connection');

var connString = "";
var app = express();

app.get('/connectionString/:id', connection.getConnString, function(res){
res.on('response', function(response) {
    res.on('data', function(chunk) {
        connString += chunk;
    console.log("The connection String is: " + connString);
    });
});
});
app.listen(3333);

截至目前,以下代码确实返回了正确的连接信息,但我需要将此数据存储在(genericserver.js)服务器级别的变量中。我认为在调用 connection.GetConnString 中间件之后,我可以使用函数(res)在 genericServer.js 中捕获响应。到目前为止,似乎中间件处理,发回响应,但函数 (res) {} 的回调从未发生。

关于如何在仍然使用 app.get() 的同时将 connection.getConnString 响应存储到 genericServer 调用中的变量中的任何想法?

var sql = require('msnodesql');
var app = require('express');

exports.getConnString = function(req, res) {

sql.query(conn_str, "SELECT DatabaseLocation, URL FROM dbo.Hospitals WHERE URL = '" + req.params.id + "'", function(err, results) {
    if (err) {
        console.log("Error running query!");
        console.log(err); return;
    };
    res.writeHead(200, {
    'Content-Type': 'text/plain'
});

    var connectionString = "";
    switch (results[0].DatabaseLocation){
        case "172.16.42.243":
            connectionString = '\"Driver={SQL Server Native Client 11.0};Server=DevelopmentSQL1;Initial Catalog={' + results[0].URL + '};Database={' + results[0].URL + '};UID={userID};PWD={pssWrd};\";'
            break;
        case "172.16.42.244":
            connectionString = '\"Driver={SQL Server Native Client 11.0};Server=DevelopmentSQL2;Initial Catalog={' + results[0].URL + '};Database={' + results[0].URL + '};UID={userID};PWD={pssWrd};\";'
            break;
        case "172.16.42.245":
            connectionString = '\"Driver={SQL Server Native Client 11.0};Server=DevelopmentSQL3;Initial Catalog={' + results[0].URL + '};Database={' + results[0].URL + '};UID={userID};PWD={pssWrd};\";'
            break;
        case "172.16.42.246":
            connectionString = '\"Driver={SQL Server Native Client 11.0};Server=DevelopmentSQL4;Initial Catalog={' + results[0].URL + '};Database={' + results[0].URL + '};UID={userID};PWD={pssWrd};\";'
            break;
        case "172.16.42.247":
            connectionString = '\"Driver={SQL Server Native Client 11.0};Server=DevelopmentSQL5;Initial Catalog={' + results[0].URL + '};Database={' + results[0].URL + '};UID={userID};PWD={pssWrd};\";'
            break;
}
    console.log(connectionString);
    res.end(connectionString);
});

};

4

1 回答 1

1

使用响应传递数据不是可行的方法。在您的中间件中,您可以将连接字符串设置为以下属性req

exports.getConnString = function(req, res, next) {
  sql.query(..., function(err, results) {
    ... // get the connection string as per your code
    req.connectionString = connectionString;
    next(); // call the next middleware/route
  });
};

接下来将调用您的路由处理程序,您可以在那里访问该属性:

app.get('/connectionString/:id', connection.getConnString, function(req, res) {
  console.log("The connection String is: ", req.connectionString);
  res.send(req.connectionString);
});
于 2013-03-22T20:13:47.083 回答