title: Stopping the middleware chain in express4
body: <p>Below is the code which has a middleware function "checkLogin" added. The eheckLogin middleware should redirect to login page if the user is not logged in. If the user is logged in it should render the requested page. </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var mongoUri = 'mongodb://localhost/mydb';
  var MongoClient = require('mongodb').MongoClient;
  
  function mongoDBConnect(req, res, next) {
      MongoClient.connect('mongodb://localhost/gadda_db', function(err, db) {
      if(!err) {
          req.db = db;
          next();
      } else {
          res.send('unable to connect to mongodb: err = ' + err);
      }
      });
  };
  
  function checkLogin(req, res, next) {
      if (req.loggedin) {
      next();
      return;
      }
  
      if (req.loggedin &amp;&amp; req.url === '/login') {
      res.redirect('http://' + 'localhost:3000' + '/game/my_game'); 
      return;
      }
  
      if (!req.loggedin &amp;&amp; req.url === '/login') {
      next();
      return;
      }
      req.db.collection('game_users', function (err, collection) {
          if (err) {
          res.send("error while reading game_users: err " + err);
      }
          collection.findOne({user: req.cookies.user, password: req.cookies.password},
                 function (err, user) {
                     if (err) {
                     res.send("error here");
                     return;
                     }
                     req.loggedin = true;
                     next();
                     return;
                 });
      });
  
      res.redirect('login');
      return;
  };
  
  router.use(mongoDBConnect);
  router.use(checkLogin);
  
  router.get('/', function(req, res) {
      res.redirect('http://' + 'localhost:3000' + '/game/my_game');
  });
  
  router.get('/login', function(req, res) {
      res.render('gadda_login', {title: 'gadda', error: ''});
  });
  </code></pre>
  
  <p>When the request to localhost:3000/gadda is sent the following error message is printed on the server </p>
  
  <blockquote>
    <p>GET /gadda/ 302 34ms - 66b Error: Can't set headers after they are
    sent.
        at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
        at ServerResponse.res.set.res.header (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/response.js:551:10)
        at ServerResponse.res.send (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/response.js:132:12)
        at fn (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/response.js:778:10)
        at View.exports.renderFile [as engine] (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/ejs/lib/ejs.js:318:3)
        at View.render (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/view.js:76:8)
        at Function.app.render (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/application.js:519:10)
        at ServerResponse.res.render (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/response.js:782:7)
        at Layer.module.exports [as handle] (/home/hhk/src/nodejs_projects/gadda_v2/app.js:54:9)
        at trim_prefix (/home/hhk/src/nodejs_projects/gadda_v2/node_modules/express/lib/router/index.js:252:17)
    router.get /login GET /gadda/login 200 11ms - 546b GET
    /stylesheets/style.css 200 4ms - 110b</p>
  </blockquote>