server.js 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. const dotenv = require('dotenv');
  2. const app = require('./app');
  3. //---------------------------------------------------------------------------//
  4. // ERROR HANDLING IN NODEJS
  5. //---------------------------------------------------------------------------//
  6. // for synchronus errors that are not caught anywhere. Should come before any code
  7. process.on('uncaughtException', err => {
  8. console.log('\x1b[31m%s\x1b[0m', '🔴️ ERROR:', err);
  9. process.exit(1);
  10. });
  11. // for rejected promises that aren't caught anywhere
  12. process.on('unhandledRejection', err => {
  13. console.log('\x1b[31m%s\x1b[0m', '🔴️ ERROR:', err);
  14. server.close(() => process.exit(1)); // shutting the system down gracefully
  15. });
  16. //---------------------------------------------------------------------------//
  17. // LOADING CONFIG FILE VARIABLES
  18. //---------------------------------------------------------------------------//
  19. dotenv.config({ path: './config.env' }); // loading .env variables
  20. //---------------------------------------------------------------------------//
  21. // STARTING SERVER
  22. //---------------------------------------------------------------------------//
  23. const port = process.env.PORT || 3000;
  24. const server = app.listen(port, () =>
  25. console.log(
  26. '\x1b[36m%s\x1b[0m', // for colors lol. Search for ansi escape characters
  27. `🎧️ listening at port ${port}. Env: ${process.env.NODE_ENV}`
  28. )
  29. );