app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * app.js
  3. *
  4. * Use `app.js` to run your app without `sails lift`.
  5. * To start the server, run: `node app.js`.
  6. *
  7. * This is handy in situations where the sails CLI is not relevant or useful.
  8. *
  9. * For example:
  10. * => `node app.js`
  11. * => `forever start app.js`
  12. * => `node debug app.js`
  13. * => `modulus deploy`
  14. * => `heroku scale`
  15. *
  16. *
  17. * The same command-line arguments are supported, e.g.:
  18. * `node app.js --silent --port=80 --prod`
  19. */
  20. // Ensure we're in the project directory, so relative paths work as expected
  21. // no matter where we actually lift from.
  22. process.chdir(__dirname);
  23. const cluster = require('cluster'),
  24. numCPUs = require('os').cpus().length;
  25. if (cluster.isMaster) {
  26. // Fork workers.
  27. for (let i = 0; i < numCPUs; i++) {
  28. cluster.fork();
  29. }
  30. cluster.on('exit', (worker, code, signal) => {
  31. if (signal) {
  32. console.log(`worker ${worker.process.pid} was killed by signal: ${signal}`);
  33. } else if (code !== 0) {
  34. console.log(`worker ${worker.process.pid} exited with error code: ${code}`);
  35. } else {
  36. console.log(`worker ${worker.process.pid} success!`);
  37. }
  38. });
  39. } else {
  40. // Ensure a "sails" can be located:
  41. (function() {
  42. var sails;
  43. try {
  44. sails = require('sails');
  45. } catch (e) {
  46. console.error('To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.');
  47. console.error('To do that, run `npm install sails`');
  48. console.error('');
  49. console.error('Alternatively, if you have sails installed globally (i.e. you did `npm install -g sails`), you can use `sails lift`.');
  50. console.error('When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,');
  51. console.error('but if it doesn\'t, the app will run with the global sails instead!');
  52. return;
  53. }
  54. // Try to get `rc` dependency
  55. var rc;
  56. try {
  57. rc = require('rc');
  58. } catch (e0) {
  59. try {
  60. rc = require('sails/node_modules/rc');
  61. } catch (e1) {
  62. console.error('Could not find dependency: `rc`.');
  63. console.error('Your `.sailsrc` file(s) will be ignored.');
  64. console.error('To resolve this, run:');
  65. console.error('npm install rc --save');
  66. rc = function () { return {}; };
  67. }
  68. }
  69. // Start server
  70. sails.lift(rc('sails'));
  71. })();
  72. }