最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 remended way to do this or am I on the right track?

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 remended way to do this or am I on the right track?

Share Improve this question asked Dec 17, 2013 at 19:48 todd.pundtodd.pund 6892 gold badges12 silver badges37 bronze badges 5
  • I'm not sure whether I understood you. Putting the number-getting functions in paranthesis looks a bit strange. First I thought this is an IIFE. Generally calling a function to set the value of a JSON object is perfect. Or did I miss your question!? – hgoebl Commented Dec 17, 2013 at 20:00
  • No, you got my question. I just wasn't sure if invoking the method from within the same file was the norm for node.js. – todd.pund Commented Dec 17, 2013 at 20:05
  • What are you doing in javascript and data calls? If these calculations involve any async (io/db) operations, it won't work as you expect them to, otherwise your approach looks ok. – vmx Commented Dec 17, 2013 at 20:06
  • But be careful when you have asynchronous functions! They don't return values but transport their oute through a callback function. – hgoebl Commented Dec 17, 2013 at 20:06
  • There is nothing asynchronous yet, but if I were to try to add the asynchronous functions, what would be the best way to do this? – todd.pund Commented Dec 17, 2013 at 20:08
Add a ment  | 

3 Answers 3

Reset to default 3

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)

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
    });
  });
};

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论