2

High Level Overview:

I have a nodejs expressjs server that makes use of the PostgreSQL nodejs driver called pg. I have a html file served to the client browser that when you click a button invokes a expressjs route on my expressjs server. This route will select data out of a PostgreSQL database and return it to the client html file. The problem is the route emits a response for every row selected in the query to the database. All the client (html browser) gets is the first row. I can write to the nodejs console server side and get all the rows to be displayed, but obviously that does me know good on my webpage.

Question: How do I get my client html file to write to console on the client for every row emitted out of my expressjs route /pg? My assumption was on the client that the onSuccess would be fired for every row emitted out of my expressJS route.

NodeJS\ExpressJS Server File:

var express = require('express');
var pg = require('pg');

var app = express();

var MemoryStore = express.session.MemoryStore;

var conString = "postgres://joe_user:password@localhost/dev_db";

var client = new pg.Client(conString);
client.connect();



app.get('/home', function(req,res){
 res.sendfile(__dirname + '/views/index.html');
});
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use
(
express.session
(
{ 
    key: 'some-key',
    secret: 'some-We1rD sEEEEEcret!',
    cookie: { secure: true },
    store: new MemoryStore({ reapInterval: 60000 * 10 }) 
}
)
);



app.use
(
function(err, req, res, next)
{
  // logic
  console.error(err.stack);
  res.send(500, 'Something broke!');
}
);

app.get('/pg', function(req, res)
{


   var  query = client.query("SELECT * FROM junk_data;"); //Returns 7000 rows with 8 columns total


query.on('row', function(row) 
{
   res.send(row);
   console.log(row);
}

);



}

);

process.on('uncaughtException', function (err) {
    console.log(err);
});

app.listen(4000);

HTML File:

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Demo: dojo/request/xhr</title>
        <!--<link rel="stylesheet" href="style.css" media="screen">-->
        <!--<link rel="stylesheet" href="../../../resources/style/demo.css" media="screen">-->
    </head>
    <body>
        <h1>Demo: dojo/request/xhr</h1>
        <p>Click the button below to see dojo/request/xhr in action.</p>
        <div>

            <button id="textButton2" onclick="SubmitPGRequest();">Call Express Route For PostgreSQL</button>
            <!--<input type="button" value="Submit" onclick="SubmitRequest();"/> -->
        </div>
        <br /><br />
        <div id="resultDiv">
        </div>
        <!-- load dojo and provide config via data attribute -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script>
        <script>

function SubmitPGRequest()
{
            new Ajax.Request( '/pg', {
              method:  'get',
              onSuccess:  function(response){
                <!--alert(response.responseText); -->
                if(response.done) {
                alert('Done!');
                }else {
                console.log(response.responseText);}
              },
              onFailure:  function(){
                alert('ERROR');
              }
});
}
        </script>
    <

/body>

4

1 回答 1

0

Ok so I figured out an answer to my question.

With the nodejs pg driver you can just send all the rows in the result object once the query has completed(end) and send them in one go. Below is what I ended up having in my expressjs server route to send all rows back the client.

//fired once the function above has completed firing.    
query.on('end',(result) 
{
    res.send(result.rows);//send all rows back to client.
}
于 2012-10-20T22:21:10.987 回答