www 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env node
  2. // support for .env file to get loaded in to environment variables.
  3. const path = require('path');
  4. const fs = require('fs');
  5. const envFile = path.join(__dirname, '../.env');
  6. try {
  7. fs.accessSync(envFile, fs.F_OK);
  8. require('dotenv').config({path: envFile});
  9. } catch (e) {
  10. // no env file
  11. }
  12. /**
  13. * Module dependencies.
  14. */
  15. const app = require('../app');
  16. const debug = require('debug')('slack-invite-automation:server');
  17. const http = require('http');
  18. /**
  19. * Normalize a port into a number, string, or false.
  20. * @param {int} val
  21. */
  22. function normalizePort(val) {
  23. const port = parseInt(val, 10);
  24. if (isNaN(port)) {
  25. return val;
  26. } else if (port >= 0) {
  27. return port;
  28. }
  29. return false;
  30. }
  31. /**
  32. * Get port from environment and store in Express.
  33. */
  34. const port = normalizePort(process.env.PORT || '3000');
  35. app.set('port', port);
  36. /**
  37. * Create HTTP server.
  38. */
  39. const server = http.createServer(app);
  40. /**
  41. * Listen on provided port, on all network interfaces.
  42. */
  43. server.listen(port);
  44. server.on('error', onError);
  45. server.on('listening', onListening);
  46. /**
  47. * Event listener for HTTP server "error" event.
  48. */
  49. function onError(error) {
  50. if (error.syscall !== 'listen') {
  51. throw error;
  52. }
  53. // handle specific listen errors with friendly messages
  54. switch (error.code) {
  55. case 'EACCES':
  56. console.error('Port ' + port + ' requires elevated privileges');
  57. process.exit(1);
  58. break;
  59. case 'EADDRINUSE':
  60. console.error('Port ' + port + ' is already in use');
  61. process.exit(1);
  62. break;
  63. default:
  64. throw error;
  65. }
  66. }
  67. /**
  68. * Event listener for HTTP server "listening" event.
  69. */
  70. function onListening() {
  71. debug('Listening on port ' + server.address().port);
  72. }