1

I write in node.js an html page and I want to wain in the response to the data but it to faster.. how can I create this function sync?

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

text = fs.readFileSync('\start.html','utf8');
text = text + '<table border="1"><tr><td>id</td><td>name</td><td>last name</td><td>address</td><td>phone</td></tr>';

pool.getConnection(function(err, connection) {
    var sql = 'select * from guides;';
    console.log(sql);
    connection.query( sql, function(err, rows) {    
        if (rows.length  > 0) {
            rows.forEach(function(row) {
            console.log('add');
                text = text + '<tr><td>' + row.id + '</td>';
                text = text + '<td>' + row.name + '</td>';
                text = text + '<td>' + row.lastName + '</td>';
                text = text + '<td>' + row.address + '</td>';
                text = text + '<td>' + row.phone + '</td></tr>';
            });
        }
        connection.end();
    });
});
text = text + '</table>'
text = text + fs.readFileSync('\end.html','utf8');

res.end(text);
});
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
royb
  • 693
  • 3
  • 9
  • 20

3 Answers3

1

try this;

app.get('/showGuides', function (req, res) {
  fetchGuides(function (err, guides) {
    if (!err && guides) {
      var text = fs.readFileSync('\start.html', 'utf8');
      text += '<table border="1"><tr><td>id</td><td>name</td><td>last name</td><td>address</td><td>phone</td></tr>';
      text += guides;
      text += '</table>';
      text += fs.readFileSync('\end.html', 'utf8');
      res.end(text);
    } else {
      res.end('Unable to fetch guides');
    }
  });
});

function fetchGuides(cb) {
  pool.getConnection(function (err, connection) {
    var sql = 'select * from guides;';
    console.log(sql);
    var text = '';
    connection.query(sql, function (err, rows) {
      if (rows.length) {
        rows.forEach(function (row) {
          console.log('add');
          text += '<tr><td>' + row.id + '</td>';
          text += '<td>' + row.name + '</td>';
          text += '<td>' + row.lastName + '</td>';
          text += '<td>' + row.address + '</td>';
          text += '<td>' + row.phone + '</td></tr>';
        });
      }
      cb(err, text);
      connection.end();
    });
  });
}
vmx
  • 7,989
  • 4
  • 22
  • 20
  • its not work.. the line text += +fs.readFileSync('\end.html', 'utf8'); is not run.. =( – royb Dec 04 '13 at 19:32
  • Can you check again, there was a typo I just fixed. – vmx Dec 04 '13 at 19:34
  • it work!!! thanks =) what you change? – royb Dec 04 '13 at 19:43
  • The `+` symbol before fs.readFileSync was extra, removed it. Apart from refactoring your code into two routines, I just moved the response sending code inside the query callback -- all that was needed. Are you comfortable with `jade` or `ejs` or any template script? Dynamic content are best rendered using templates, I can illustrate that modifying your code further, if you're interested. – vmx Dec 04 '13 at 20:07
  • yes it can be grate.. i read about them but not start use them.. – royb Dec 04 '13 at 20:19
0

Hum it seems that you just want to render a dynamic html content. In this case, you may really prefer to use a template engine, it would be much simpler. As you are already using Express, this should help:

Anyway with your approach, you have to reply to the client request (i.e. res.end) inside the query callback.

Ervadac
  • 936
  • 3
  • 9
  • 26
0

I had updated your code to make it work in my earlier post. Hope you're able to follow it well. However, dynamic content such as results retrieved from db are best handled using templates, such as jade or ejs.

I will refactor your code to demonstrate how we can do that in express using jade template. I have stubbed a few things to make the demo work, you can skip/modify them as you like. :)

Stand alone script to handle /showGuides request:

var fs = require('fs');
var util = require('util');
var path = require('path');
var http = require('http');
var express = require('express');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// configure other middlewares you need

app.use(app.router);

process.on('uncaughtException', console.log);

var guides = {
  process: function process(req, res, next) {
    util.log('Received request to fetch guides');
    guides.fetch(function (err, guides) {
      req.guides = guides || [];
      next(err);
    });
  },

  fetch: function fetch(cb) {
    pool.getConnection(function (err, connection) {
      var sql = 'select * from guides;';
      connection.query(sql, function (err, rows) {
        cb(err, rows);
        connection.end();
      });
    });
  },

  render: function render(req, res, next) {
    res.locals.guides = req.guides;
    res.render('guides');
  },

  errors: function errors(err, req, res, next) {
    console.log(err);
    res.locals.errmsg = err.message;
    res.render('error');
  }
};

app.get('/showGuides', guides.process, guides.render, guides.errors);

var server = http.createServer(app).listen(3000);

// dummy pool -- you don't need this
var pool = {
  connection: {
    query: function (query, cb) {
      // dummy response -- simulate a db call
      setTimeout(function () {
        var dummies = [{
          id: 1,
          name: 'test',
          lastName: 'test',
          address: 'test',
          phone: 1234
        }, {
          id: 2,
          name: 'test2',
          lastName: 'test2',
          address: 'test2',
          phone: 12345
        }];
        cb(null, dummies);
      }, 500);
    },
    end: function () {
      // nothing to do
    }
  },

  getConnection: function (cb) {
    cb(null, pool.connection);
  }
};

Now we need to add some jade templates, namely guides.jade, start.jade, end.jade and error.jade. Add these files under dir views.

views/guides.jade:

include start

table(border="1", cellspacing="0", cellpadding="5px", width="50%")
  tr
    th id
    th name
    th last name
    th address
    th phone

  each row in guides
    tr
      td= row.id
      td= row.name
      td= row.lastName
      td= row.address
      td= row.phone

include end

views/start.jade

//- Contents from start.html

h3 Guides

views/end.jade

//- contents from end.html

p Guides data rendered.

views/error.jade

h3 Oops! An error occurred

p= errmsg

If you're new to jade, start with jade tutorial. Let me know if you're unable to follow anything in the code above.

Good Day!

vmx
  • 7,989
  • 4
  • 22
  • 20