title: Node.js / express: respond immediately to client request and continue tasks in nextTick
body: <p>I would like to separate server high consuming CPU task from user experience:</p>
  
  <p>./main.js:</p>
  
  <pre><code>var express = require('express');
  var Test = require('./resources/test');
  var http = require('http');
  var main = express();
  
  main.set('port', process.env.PORT || 3000);
  main.set('views', __dirname + '/views');
  main.use(express.logger('dev'));
  main.use(express.bodyParser());
  main.use(main.router);
  
  main.get('/resources/test/async', Test.testAsync);
  
  main.configure('development', function() {
    main.use(express.errorHandler());
  });
  
  http.createServer(main).listen(main.get('port'), function(){
    console.log('Express server app listening on port ' + main.get('port'));
  });
  </code></pre>
  
  <p>./resources/test.js:</p>
  
  <pre><code>function Test() {}
  module.exports = Test;
  
  Test.testAsync = function(req, res) {
    res.send(200, "Hello world, this should be sent inmediately");
    process.nextTick(function() {
      console.log("Simulating large task");
      for (var j = 0; j &lt; 1000000000; j++) {
        // Simulate large loop
      }
      console.log("phhhew!! Finished!");
    });
  };
  </code></pre>
  
  <p>When requesting "localhost:3000/resources/test/async" I would expect the browser rendering "Hello world, this should be sent inmediately" really fast and node.js to continue processing, and after a while in console appearing "finished" message.</p>
  
  <p>Instead, browser keeps waiting until node.js finishes large task and then renders the content. I've tried with <code>res.set({ 'Connection': 'close' });</code> and also <code>res.end();</code> but nothing works as expected. I've also googled with no luck.</p>
  
  <p>How should it be to send the response to client immediately and server continue with tasks?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>posted fork method in solution</p>