Structuring code to do URL routing for Node.js What would be the best (performance, memory wise) to achieve a clean way to create a class or some way to properly structure the code, that shares 2 variables (req, res) which have a decent object size.

And yes, for those who use Node.js it are the req and res variables, but it is irrelevant.

This is what I tried so far:

function Client(req, res){
  var self = this;

  self.req = req;
  self.res = res;

  route.call(self);
  handleRoute.call(self);
  handleRequestData.call(self);
}

function route(){
  var self = this;

  self.req.route = // ...
}

function handleAuth(){
  var self = this;

  self.req.auth = // ...
}

function handleRequestData(){
  var self = this;

  self.req.data = // ...
}


I am wondering if it could be improved, since I mentioned req and res are pretty decent objects with methods, properties. Since .call(self) you pass through the instance, would it be most effective way?

Also I don't like using "var self = this;" all the time, it is useless. 

And btw, I don't want to use Coffeescript.

Solution 1. (fails because of the massive passing through req and res)

Lets assume we got to files 1. client.js

var Client = {
  route: function(req, res){},
  auth: function(req, res, callback){ callback(req, res); },
  getData: function(req, res, callback){ callback(req, res); } // async
}


file 2. main.js

var client = require('./client.js');

client.route(req, res); // fine because this is syncronous.

// This is where the problem is, massive passing through req and res parameters.
client.auth(req, res, function(req, res){
  client.getData(req, res, function(req, res){
    // Do something...
  });
});
Are you intending to reuse the route, handleAuth, and handleRequestData functions? They could be "private" to the Client constructor function:

function Client(req, res) {
    function route() {
        //...
    }

    function handleAuth() {
        //...
    }

    function handleRequestData() {
        //...
    }

    route();
    handleRoute();
    handleRequestData();
}


See that I didn't set req and res as this members. I don't know if this is mandatory in your case. Also, this is just the beginning of what could be done; as stated in the comments to this answer, every time a new Client is created, new instances of the private functions are created as well, wasting resources.

A more sophisticated approach could define Client using a self-invoking function expression:

var Client = (function() {
    function route(req, res) {
        //...
    }

    function handleAuth(req, res) {
        //...
    }

    function handleRequestData(req, res) {
        //...
    }

    // This is the constructor function:
    return function(req, res) {
        route(req, res);
        handleRoute(req, res);
        handleRequestData(req, res);
    }
})();


Here, Client is defined as the product of the function expression enclosed in the outermost parenthesis. The function expression returns the constructor function, which has access to the closure functions route, handleRoute, and handleRequestData. Note that each of these functions is defined with req and res parameters. This way, they behave like static private functions, and you can code and use them regardless of what's referenced by this.

About self = this: it's known that JavaScript may be very confusing about the this keyword. Some information here. So, sometimes it's convenient to assign this to a closure variable named like self or me. It should not be done blindly though... as anything after all.