title: How do I pass data returned from a controller to Express' router?
body: <p>I'm trying to make a catch-all of sorts to return data to my Author endpoint. If the url that is passed to the endpoint contains no query parameters, I want the router to return the full list of authors available. If the url contains firstName and lastName parameters, I want the controller to find the authors that match and, pass that data back to the router.</p>
  
  <p>Currently if I send the urls <code>http://localhost:3001/authors</code> or <code>http://localhost:3001/authors?firstName=tom&amp;lastName=dooly</code>, I get an error <code>Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client</code>.</p>
  
  <p>Can anyone tell me why this is happening and how to fix it?</p>
  
  <p>main:</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  
  var app = express();
  var dev_db_url = 'mongodb://localhost:27017/'
  var mongoDB = process.env.MONGODB_URI || dev_db_url;
  
  mongoose.connect(dev_db_url);
  
  mongoose.Promise = global.Promise;
  var db = mongoose.connection;
  db.on('error', console.error.bind(console, 'MongoDB connection error:'));
  
  
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  
  var index = require('./routes/index');
  var users = require('./routes/users');
  var feedEntries = require('./routes/feedEntries');
  var authors = require('./routes/authors');
  
  
  app.use('/', index);
  app.use('/users', users);
  app.use('/feedEntries', feedEntries);
  app.use('/authors', authors);
  
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not not Found');
    err.status = 404;
    next(err);
  });
  
  
  app.use(function(err, req, res, next) {
  
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
  
    res.status(err.status || 500);
    res.render('error');
  });
  
  module.exports = app;
  </code></pre>
  
  <p>route: </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  var authorController = require('../controllers/authorController');
  
  authorController.findAuthorsByFirstAndLastName);
  
  
  router.get('/', function (req, res) {
      if(req.query.firstName||req.query.lastName) {
          res.send(authorController.findAuthorsByFirstAndLastName(req,res));
      }else{
          res.send(authorController.author_list(req,res));
      }
    });
  
  
  module.exports = router;
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>var Author = require('../models/author')
  var async = require('async')
  
  
  exports.author_list = function(req, res, next) {
  
      Author.find({},function(err, authors) {
          if (err){
              res.send(err);
          }
              return.json(authors);
      });
  
    };
  
    exports.findAuthorsByFirstAndLastName = function (req, res, next){
      var query = {}
  
      if(req.query.firstName||req.query.lastName) {
  
          query = {$or:[{firstName:{$regex: req.query.firstName, $options: 'i'}},
              {lastName:{$regex: req.query.lastName, $options: 'i'}}]}
      }
  
      else {
          return res.status(500).send({ error: 'Unable to parse data'});
      }
  
      var firstName =  req.body.firstName;
      var lastName = req.body.lastName;
  
      Author.find(query , function (err, authors) {
          if(err) {
              res.send(err);
          }
          res.json(authors);
       });
    };
  </code></pre>