2013-03-22 2 views
0

전달 된 URL을보고 올바른 데이터베이스 연결 문자열을 반환하는 node.js 응용 프로그램을 만드는 중입니다. 아래는 내 genericServer.js 파일입니다. 두 번째 코드 블록은 내 Connection.js 파일입니다. id가 URL에서 genericServer 파일로 전달되면이를 미들웨어 connection.getConnString에 전달합니다.변수 node.js에 함수 호출 값을 저장하는 방법

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 미들웨어를 호출 한 후 genericServer.js에서 응답 (res)을 사용하여 응답을 잡을 수있을 것이라고 생각했습니다. 지금까지는 미들웨어 프로세스가 응답을 되돌려 보내는 것으로 보이지만 함수 (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

응답을 사용하여 데이터를 전달하는 것은 방법이 아닙니다.

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 
    }); 
}; 

귀하의 경로 핸들러가 다음 전화를받을 것입니다, 당신은 거기에 속성에 액세스 할 수 있습니다 : 미들웨어, 당신은 req의 속성으로 연결 문자열을 설정할 수

app.get('/connectionString/:id', connection.getConnString, function(req, res) { 
    console.log("The connection String is: ", req.connectionString); 
    res.send(req.connectionString); 
}); 
+0

감사 robertklep을! –

관련 문제