todd.pund
todd.pund

Reputation: 689

node.js - express - res.render() : Correct format for feeding variables into the JSON parameter?

I'm learning node.js so bear with me.

I'm trying to create a node.js web application using express+jade that is basically just a line queue. (I.E. take a number, wait in line, now serving number 4...except the 4 will be a mysql table field). The page will auto-update every 5 seconds. There are three line queues handled by the page (I.E) :3000/1 :3000/2 :3000/3.

To be clear, I have the application working, but I want to make sure I am doing it correctly as opposed to just hacking it together with poor methodology.

In my index.js I have the standard setup:

exports.bio = function(req, res){
  res.render('index', { 
    location: 'Biometrics',
    number: bio()
  });
};

exports.interview = function(req, res){
  res.render('index', { 
    location: 'Interview',
    number: interview()
  });
};

exports.docs = function(req, res){
  res.render('index', { 
    location: 'Documentation',  
    number: doc()
  });
};

I am currently also calling the values for the "number:" JSON value from within the index.js as well.

var doc = (function() {
  //do javascript and data calls
  return a;
});
var bio = (function() {
  //do javascript and data calls
  return a;
});
var interview = (function() {
  //do javascript and data calls
  return a;
});

My question is: What would be the recommended way to do this or am I on the right track?

Upvotes: 1

Views: 9679

Answers (3)

vmx
vmx

Reputation: 8407

Say if you had an async operation in bio();

exports.bio = function (req, res) {
  bio(function (err, data) {
    if (!err && data) {
      res.render('index', {
        location: 'Biometrics',
        number: data
      });
    } else {
      // handle error or no data
      res.render('error');
    }
  });
}

var bio = function(cb) {
  //do javascript and data calls
  cb(err, data);
});

Again, there are many ways to get this working. But the above should do.

Upvotes: 0

hgoebl
hgoebl

Reputation: 13007

The asynchonous way would be something like:

exports.docs = function(req, res) {
  fetchSomeValueFromMysql(req.param('foo'), function (err, data) {
    if (err) {
      res.send(500, 'boom!');
      return;
    }
    res.render('index', { 
      location: 'Documentation',  
      number: data
    });
  });
};

Upvotes: 0

Hector Correa
Hector Correa

Reputation: 26690

This will work as long as the functions doc(), bio(), and interview() are synchronous, but most likely that won't be the case, particularly if they need to perform some database access.

If these functions were async then your could should look like this:

exports.docs = function(req, res){
  // call the doc() function and render the response in the callback
  doc(function(err, number) {
    res.render('index', { 
      location: 'Documentation',  
      number: number
    });
  });
};

The doc() function will look like this:

var doc = (function(callback) {
  // This code very likely be async 
  // therefore it won't return any value via "return"
  // but rather calling your function (callback)
  db.doSomething(someparams, callback);
});

Inside db.doSomething() there will be a call to your function callback(err, theValue)

Upvotes: 3

Related Questions