index.js 789 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env node --harmony --trace_gc
  2. let cluster = require('cluster');
  3. let http = require('http');
  4. if (cluster.isMaster) {
  5. let num = require('os').cpus().length;
  6. num = num / 4;
  7. for (let i = 0; i < num; i++) {
  8. cluster.fork();
  9. }
  10. cluster.on('exit', function(worker, code, signal) {
  11. console.error('worker ' + worker.process.pid + ' died');
  12. cluster.fork();
  13. });
  14. cluster.on('listening', function(worker, address) {
  15. console.log("A worker with #" + worker.id + " is now connected to "
  16. + address.address + ":" + address.port);
  17. });
  18. } else {
  19. let app = require('./app.js');
  20. let port = process.env.PORT || '3000';
  21. app.set('port', port);
  22. let server = http.createServer(app);
  23. server.listen(port);
  24. }