title: ExpressJS 4 execute function after res.send()
body: <p>I am new to nodeJS and asynchronous programming. I am using express as the base for my app, there is really only one route that serves a page and accepts an upload from a form. I would like to make a POST request to an external service after the the file has been uploaded. Attempting to execute any code after res.send(200) however results in an error message of <code>Error: Can't set headers after they are sent.</code></p>
  
  <p>I am using the <a href="https://www.npmjs.com/package/request" rel="nofollow">request</a> package to make the post to the external service.</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var util = require("util");
  var request = require("request");
  
  /* POST uploads. */
  router.post('/', function(req, res, next) {
      console.log("LOG:" + util.inspect(req.files));
      res.send('respond with a resource');
      postFile(req.files.file.path);
  });
  
  var postFile = function(path) {
      var opts = {
          method: 'POST',
          uri: 'https://example.com/api/files.upload',
          formData: {
              token: "xxx",
              file: fs.createReadStream(req.files.file.path)
          }
      }
  
      // console.log("LOG:\n" + util.inspect(opts));
  
      request.post(opts, function(error, response, body) {
          if (error) {
              console.log("ERROR LOG: " + util.inspect(error));
          } else {
              console.log("RESPONSE LOG:" + util.inspect(response));
          }
      });
  }
  
  module.exports = router;
  </code></pre>
  
  <p>The postFile function works fine on it's own and even adding a console.log directly after the res.send results in the same error. How can I continue to execute code on the server after the response has been sent to the client?</p>
  
  <p>Output log from node:</p>
  
  <p><code>POST /uploads 200 85.201 ms - 23
  Error: Can't set headers after they are sent.
      at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
      at ServerResponse.header (/Users/jason/dev/test/file-share/node_modules/express/lib/response.js:700:10)
      at ServerResponse.send (/Users/jason/dev/test/file-share/node_modules/express/lib/response.js:154:12)
      at fn (/Users/jason/dev/test/file-share/node_modules/express/lib/response.js:934:10)
      at View.exports.renderFile [as engine] (/Users/jason/dev/test/file-share/node_modules/jade/lib/index.js:374:12)
      at View.render (/Users/jason/dev/test/file-share/node_modules/express/lib/view.js:93:8)
      at EventEmitter.app.render (/Users/jason/dev/test/file-share/node_modules/express/lib/application.js:566:10)
      at ServerResponse.res.render (/Users/jason/dev/test/file-share/node_modules/express/lib/response.js:938:7)
      at /Users/jason/dev/test/file-share/app.js:58:7
      at Layer.handle_error (/Users/jason/dev/test/file-share/node_modules/express/lib/router/layer.js:58:5)</code></p>