Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to capture streaming results with Prototype Ajax.request
    primarykey
    data
    text
    <p>High Level Overview:</p> <p>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.</p> <p>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.</p> <p>NodeJS\ExpressJS Server File:</p> <pre><code>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); </code></pre> <p>HTML File:</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Demo: dojo/request/xhr&lt;/title&gt; &lt;!--&lt;link rel="stylesheet" href="style.css" media="screen"&gt;--&gt; &lt;!--&lt;link rel="stylesheet" href="../../../resources/style/demo.css" media="screen"&gt;--&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Demo: dojo/request/xhr&lt;/h1&gt; &lt;p&gt;Click the button below to see dojo/request/xhr in action.&lt;/p&gt; &lt;div&gt; &lt;button id="textButton2" onclick="SubmitPGRequest();"&gt;Call Express Route For PostgreSQL&lt;/button&gt; &lt;!--&lt;input type="button" value="Submit" onclick="SubmitRequest();"/&gt; --&gt; &lt;/div&gt; &lt;br /&gt;&lt;br /&gt; &lt;div id="resultDiv"&gt; &lt;/div&gt; &lt;!-- load dojo and provide config via data attribute --&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"&gt;&lt;/script&gt; &lt;script&gt; function SubmitPGRequest() { new Ajax.Request( '/pg', { method: 'get', onSuccess: function(response){ &lt;!--alert(response.responseText); --&gt; if(response.done) { alert('Done!'); }else { console.log(response.responseText);} }, onFailure: function(){ alert('ERROR'); } }); } &lt;/script&gt; &lt; </code></pre> <p>/body> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload