Question

I'm working on creating a node.js application that will look at a URL passed over to it and then return the correct database connection string. Below is my genericServer.js file. The second code block is my Connection.js file. When an id is passed over in the URL to the genericServer file, I then pass it to the middleware 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);

As of now the below code DOES return the correct connection information but I need to store this data off in a variable at the (genericserver.js) server level. I thought that I would be able to catch the response in the genericServer.js with the function(res) after the call to connection.GetConnString middleware. So far it seems that the middleware processes, sends back the response, but then the callback to function (res) {} never occurs.

Any ideas on how I can store the connection.getConnString response into a variable in the genericServer call while still using app.get()?

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

};

Was it helpful?

Solution

Passing data around using responses isn't the way to go. In your middleware, you could set the connection string as a property of 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
  });
};

Your route handler would get called next, and you can access the property there:

app.get('/connectionString/:id', connection.getConnString, function(req, res) {
  console.log("The connection String is: ", req.connectionString);
  res.send(req.connectionString);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top