2

I am trying to get a simple info from db into a node.js http server response. In the following snippet I can see the DB results in the system log, but not in the http response.

Can you give me some ideas on why this is happening?

Thanks.

var
    sys = require( 'sys' )
    , http = require( 'http' )
    , dbParams = {
          user : 'test'
        , pass : 'test'
        , db : 'just_test'
    }
;

function dbConnect() {
    var Client = require( 'mysql' ).Client
        , client = new Client()
    ;

    client.user = dbParams.user;
    client.password = dbParams.pass;
    client.connect();
    client.query('USE ' + dbParams.db);

    return( client );
}

var dbClient = dbConnect();

http.createServer( function( httpRequest, httpResponse ) {
    httpResponse.writeHead( 200, { 'Content-Type' : 'text/plain' } );
    httpResponse.write( '=== START httpResponse' + "\n" );

    dbClient.query( 'SELECT * FROM base_events', function (err, dbRes, fields) {
        if (err) { throw err; }

        httpResponse.write( 'Obtained: ' + JSON.stringify( dbRes ) );
sys.log( 'FROM DB: ' + JSON.stringify( dbRes ) );
    } );

    httpResponse.write( '=== Test' + "\n" );
    httpResponse.end();

    dbClient.end();
} ).listen( 8000 );

sys.puts( 'Server running at http://127.0.0.1:8000' );
Georgiana
  • 323
  • 1
  • 4
  • 9

1 Answers1

5

The async callback for client.query is getting called after you've ended the httpResponse. Try moving the last few statements inside that callback - e.g:

http.createServer( function( httpRequest, httpResponse ) {
    httpResponse.writeHead( 200, { 'Content-Type' : 'text/plain' } );
    httpResponse.write( '=== START httpResponse' + "\n" );

    dbClient.query( 'SELECT * FROM base_events', function (err, dbRes, fields) {
        if (err) { throw err; }

        httpResponse.write( 'Obtained: ' + JSON.stringify( dbRes ) );
    sys.log( 'FROM DB: ' + JSON.stringify( dbRes ) );

        httpResponse.write( '=== Test' + "\n" );
        httpResponse.end();

        dbClient.end();

    } );

} ).listen( 8000 );
Community
  • 1
  • 1
Geoff Chappell
  • 2,432
  • 1
  • 16
  • 12
  • thanks, that worked great do you have any idea how I can get the db information out of the callback for later processing? or do I just stuff everything in that callback, before the `httpResponse.end()` ? – Georgiana May 21 '11 at 12:24
  • @Geo you need to stuff everything in that callback but you can use OO or EventEmitter to make the code more loosely coupled. – Raynos May 21 '11 at 12:28
  • @geo I usually think in terms of a state machine when I'm working with async problems -- where the async callbacks are the things that move you between states. Sometimes the state machine is simple enough that you can just use nested callbacks; other times that gets unworkable and you need to use some other pattern to make the code manageable. – Geoff Chappell May 21 '11 at 12:40
  • thanks Geoff for the thoughts, I am a beginner in the node.js mindset therefore it is a bit harder to grasp all the goodies so fast; the EventEmitter is not a trivial concept to grasp, but I am understanding more and more its benefits – Georgiana May 26 '11 at 19:43