- Asynchronous response.write() is not working
- <html><head><title>ddc Test</title></head><body><table></table></body></html>
- var http = require('http');
- var db = require('./dyDB');
- var strHTML = '<html><head><title>ddc Test</title></head><body><table>';
- db.connect(initServer);
- function initServer()
- {
- http.createServer(function (req, res) {
- res.writeHead(200, {'Content-Type' : 'text/html'});
- dbConnection(res);
- }).listen("8888","127.0.0.1");
- console.log("Server started");
- }
- function dbConnection(res)
- {
- res.write(strHTML);
- db.dbcon1(
- function(data){dataHandler(res,data);},
- function(err){errorHandler(res,err);},
- function(){endConnection(res);}
- );
- }
- function errorHandler(res, error){}
- function dataHandler(res, data)
- {
- res.write("<tr>");
- for(var ind in data)
- res.write("<td>" + data[ind] + "</td>");
- res.write("</tr>");
- }
- function endConnection(res)
- {
- res.end("</table></body></html>");
- }
- var Connection = require('tedious').Connection;
- var Request = require('tedious').Request;
- var config = {
- userName: 'sa',
- password: 'password',
- server: 'localhost',
- database: 'testdb'
- };
- var strStmt1 =
- "EXEC [dbo].[spListAllItems]
- @blnHideDefault = 1,
- @blnOnlyLatest = 1";
- var connection;
- exports.connect = function(cont)
- {
- connection = new Connection(config);
- connection.on('connect', function(err) {
- if(err)
- {
- console.log(err);
- }else{
- console.log("connected");
- cont(); //go on
- }
- });
- }
- exports.dbcon1 = function(data, error, finished)
- {
- request = new Request(strStmt1,function(err){error(err);});
- request.on('row', function(columns) {
- row(data,columns);
- });
- request.on('done', finished);
- connection.execSql(request);
- }
- function row(data, columns)
- {
- var arrData = [];
- for(var index in columns)
- {
- arrData.push(escape(columns[index].value));
- }
- data(arrData);
- }