Question

So, I was told that passing around the request and or response variable in nodeJS is "bad practice". But this means that most of your code has to be in the server.js file, making it cluttered and kind of ugly.

How can you modularize your nodejs server, passing around req/res appropriately and be able to organize your code into separate files?

For example, I would like to split my socket routing, .get and .post into different files, but still be able to use the callback parameters like so:

app.io.route("disconnect", function(req,res) { <--- these params
    db.query("UPDATE player_data SET online=0 WHERE id="+mysql.escape(req.session.user));
    req.io.broadcast("event", {msg:req.session.username+" has logged out!"});
    app.io.broadcast("reloadXY");
});

As of right now they're all in one file and I don't like that.

Was it helpful?

Solution

I think what the person meant by 'passing around' was something like this (in plain express):

app.get('/kittens', function(req, res) {
  db.doAthing(req);
  updateSomethingElse(res);
  upvoteThisAnswer(res);
});

That is, passing around the two variables beyond the first function. This is bad because it becomes increasingly difficult to figure out where the call actually ends. One little res.end(500) in updateSomethingElse can cause the whole house of cards to come tumbling down.

It's perfectly ok (in fact, standard to the point of being the default in express) to declare that callback elsewhere (usually the /routes directory of your project.)

// app.js

var user = require('./routes/user')
  , kittens = require('./routes/kittens');

// express stuff...

app.get('/settings', user.getSettings);
app.get('/fur', kittens.shed);

Then, in routes/user.js:

exports.getSettings = function(req, res) {
  // Note how we're passing around properties of req/res, not the objects themselves.
  db.getUserSettings(req.user.id).then(function(settings) {
    res.render('settings', settings);
  });
};

OTHER TIPS

This video from TJ Holowaychuk (the guy who wrote Express and a ton of other Node infrastructure that we all use) helped me take Express modularization to the next level. Basically you can make individual apps in their own folders and consume them as middleware very easily. I have managed to extend this technique to socket.io with some tricks.

http://vimeo.com/56166857

You should not pass req and res to another modules but pass callbacks from another modules to route. It should look like.

var someModule = require("./someModule")

app.get("/someAction", someModule.handleSomeAction) ;

If You want to have post and get in another modules You should pass reference to app (from express()) once to that module and operate on that.

For example :

var express = require("express") ;
var app = express();
var get_handler = require("./get_handler ")
var post_handler = require("./post_handler ")

get_handler.init(app);
post_handler.init(app);

and in post/get_handler :

var app;
exports.init = function( eApp){
    app = eApp;
    // operate on app 
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top