Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.99 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Asynchronous response.write() is not working
  2. <html><head><title>ddc Test</title></head><body><table></table></body></html>
  3.        
  4. var http = require('http');
  5. var db = require('./dyDB');
  6.  
  7. var strHTML = '<html><head><title>ddc Test</title></head><body><table>';
  8.  
  9. db.connect(initServer);
  10.  
  11. function initServer()
  12. {
  13.     http.createServer(function (req, res) {
  14.         res.writeHead(200, {'Content-Type' : 'text/html'});
  15.         dbConnection(res);
  16.     }).listen("8888","127.0.0.1");
  17.     console.log("Server started");
  18. }
  19.  
  20. function dbConnection(res)
  21. {
  22.     res.write(strHTML);
  23.     db.dbcon1(
  24.         function(data){dataHandler(res,data);},
  25.         function(err){errorHandler(res,err);},
  26.         function(){endConnection(res);}
  27.     );
  28. }
  29.  
  30. function errorHandler(res, error){}
  31.  
  32. function dataHandler(res, data)
  33. {
  34.     res.write("<tr>");
  35.     for(var ind in data)
  36.         res.write("<td>" + data[ind] + "</td>");
  37.     res.write("</tr>");
  38. }
  39.  
  40. function endConnection(res)
  41. {
  42.     res.end("</table></body></html>");
  43. }
  44.        
  45. var Connection = require('tedious').Connection;
  46. var Request = require('tedious').Request;
  47.  
  48.  
  49. var config = {
  50.     userName: 'sa',
  51.     password: 'password',
  52.     server: 'localhost',
  53.     database: 'testdb'
  54.   };
  55.  
  56. var strStmt1 =
  57. "EXEC   [dbo].[spListAllItems]
  58.         @blnHideDefault = 1,
  59.         @blnOnlyLatest = 1";
  60.  
  61. var connection;
  62.  
  63. exports.connect = function(cont)
  64. {
  65.     connection = new Connection(config);
  66.     connection.on('connect', function(err) {
  67.  
  68.         if(err)
  69.         {
  70.             console.log(err);
  71.         }else{
  72.             console.log("connected");
  73.             cont(); //go on
  74.         }
  75.     });
  76. }
  77.  
  78. exports.dbcon1 = function(data, error, finished)
  79. {
  80.     request = new Request(strStmt1,function(err){error(err);});
  81.     request.on('row', function(columns) {
  82.         row(data,columns);
  83.     });
  84.     request.on('done', finished);
  85.     connection.execSql(request);
  86. }
  87.  
  88. function row(data, columns)
  89. {
  90.     var arrData = [];
  91.     for(var index in columns)
  92.     {
  93.         arrData.push(escape(columns[index].value));
  94.     }
  95.     data(arrData);
  96. }