title: Adding middleware to check API key & pass
body: <p>I am just starting out learning node.js after deciding to write my next API using node and restify. There will only be a couple of users accessing the API and it will be a strictly private one - no one else should be able to access it.</p>
  
  <p>Basically, I need away to check that "key" and "pass" params are supplied (and valid) for every request. So, I thought I could add some middleware that is executed before everything else to do the checking.. e.g.:</p>
  
  <pre><code>var restify = require('restify');
  
  var server = restify.createServer({
      name: 'test'
  });
  
  server.use(function(req, res, next) {
      if (req.params.key == null) {
          console.log("No API key supplied");
          return next(new restify.NotAuthorizedError("No API key supplied"));
      }
  });
  server.use(restify.bodyParser());
  
  
  server.listen(8888, function() {
      console.log('%s listening at %s', server.name, server.url);
  });
  
  server.get('/test', function(req, res, next) {
      res.send('Hello World');
      return next();
  });
  </code></pre>
  
  <p>Browsing to <code>localhost:8888/test</code> gives <code>{"code":"NotAuthorized","message":"No API key supplied"}</code> which is what I want.</p>
  
  <p>However, browsing to <code>http://localhost:8888/test?key=a</code> still gives the same message..</p>
  
  <p>Clearly, I'm not going about this the right way. Can someone point me in the right direction?</p>
  
  <p>Thanks in advance.</p>