title: Node server can´t set headers after sent
body: <p>I have a node server and an angularJS app.</p>
  
  <p>I have a route to get informations about one record.</p>
  
  <p>The route is '/pacientes/:id'</p>
  
  <p>When I request data from that route i am getting an error.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>//error:</p>
  
  <pre><code>Error: Can't set headers after they are sent.
      at ServerResponse.setHeader (_http_outgoing.js:367:11)
      at ServerResponse.header (C:\nodeapp\cloudapp\node_modules\express\lib\respo
  nse.js:719:10)
      at ServerResponse.send (C:\nodeapp\cloudapp\node_modules\express\lib\respons
  e.js:164:12)
      at ServerResponse.json (C:\nodeapp\cloudapp\node_modules\express\lib\respons
  e.js:250:15)
      at C:\nodeapp\cloudapp\server.js:973:10
      at Array.forEach (native)
      at C:\nodeapp\cloudapp\server.js:971:13
      at Layer.handle [as handle_request] (C:\nodeapp\cloudapp\node_modules\expres
  s\lib\router\layer.js:95:5)
      at next (C:\nodeapp\cloudapp\node_modules\express\lib\router\route.js:131:13
  )
      at Route.dispatch (C:\nodeapp\cloudapp\node_modules\express\lib\router\route
  .js:112:3)
  </code></pre>
  
  <p>Here is app controller:</p>
  
  <pre><code>angular.module("clinang").controller('ProcedimentosCtrl',['$scope','$http','$state',function($scope,$http,$state){
        $scope.modelo={}
        var tipoId=$state.params.tipoId;
        if (tipoId) {
           $http.get('/pacientes/' + tipoId).then(function(response){
              $scope.modelo=response.data;
          }, function(error){
            console.log(error)
          });
        }
  }]);
  </code></pre>
  
  <p>node - server.js</p>
  
  <pre><code>    var express = require('express');
      var bodyParser = require('body-parser');
      var jwt = require('jsonwebtoken');
      var expressJwt = require('express-jwt');
      var path = require('path');
      var app = express();
  
      // Define the port to run on
      app.set('port', process.env.port || 80);
  
      app.use(bodyParser.json());
  
      app.all('*', function(req, res, next) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
        res.header('Access-Control-Allow-Headers', 'Content-Type');
        next();
      });
  
      var pacientes=[
          {id:1, nome:'Joao'},
          {id:2, nome:'Maria'}
      ];
  
  
      app.get('/pacientes/:id', function(req, res) {
        pacientes.forEach(function (paciente) {
          if (paciente.id == req.params.id) {
               res.json(paciente);
               return;
          }
        });
        res.status(404).end();
      });
  
  //The 404 Route (ALWAYS Keep this as the last route)
  app.get('*', function(req, res){
    res.status(404).end();
  });
  
  // Listen for requests
  var server = app.listen(app.get('port'), function() {
    var port = server.address().port;
    console.log('Magic happens on port ' + port);
  });
  </code></pre>