如何将函数调用值存储到变量node.js中

时间:2013-03-22 18:56:56

标签: node.js express

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

截至目前,以下代码DOES返回正确的连接信息,但我需要将此数据存储在(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);
});

};

1 个答案:

答案 0 :(得分: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);
});