【问题标题】:how to store a function call value into a variable node.js如何将函数调用值存储到变量node.js中
【发布时间】:2013-03-23 04:13:47
【问题描述】:

我正在创建一个 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);
});

};

【问题讨论】:

    标签: node.js express


    【解决方案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);
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2022-01-04
      • 2011-04-22
      • 2020-11-16
      • 1970-01-01
      • 2021-11-28
      • 2022-11-11
      • 2019-03-23
      • 1970-01-01
      相关资源
      最近更新 更多