title: Routing in Express/Node.js using multiple callbacks
body: <p>I'm new to Node.js, or even JavaScript in general, and I'm currently setting up a http-server using Node/Express that should query a postgresql database. I am unsure if I'm using the routing and multiple callbacks correct, and hope someone can clarify this a bit.</p>
  
  <p>Right now, it looks like this:</p>
  
  <p>In my <strong>web.js</strong></p>
  
  <pre><code>var routes = require('./routes');
  app.get('/get/user/:userid', routes.user, routes.dbquery);
  </code></pre>
  
  <p>and in <strong>routes/index.js</strong></p>
  
  <pre><code>exports.user = function(request, response, next) {
      request.querystring = "select * from users where userid = " + request.param('userid');
      next();
  };
  
  exports.dbquery = function(request, response) {
      pg.connect(process.env.DATABASE_URL, function(err, client, done) {
          var query = client.query(request.querystring);  
          query.on('row', function(row, result) {
              result.addRow(row);
          });
          query.on('end', function(result) {
              response.send(JSON.stringify(result.rows));
          });
          done();
      });
  };
  </code></pre>
  
  <p>It works, but is this the correct way to set up routing? Specifically, is it ok to just pass on variables like the query string inside the request variable? Or is there some other, preferred solution to this?</p>
  
  <p>The reason I want to do it is because I want to be able to reuse the dbquery function, since I will have multiple routes which will query the database.</p>