Statically generated site for Redbrick https://www.redbrick.dcu.ie
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.6 KiB

  1. #!/usr/bin/env node
  2. /**
  3. * Module dependencies.
  4. */
  5. const app = require('../app');
  6. const debug = require('debug')('express:server');
  7. const http = require('http');
  8. /**
  9. * Get port from environment and store in Express.
  10. */
  11. const port = normalizePort(process.env.PORT || '3000');
  12. app.set('port', port);
  13. /**
  14. * Create HTTP server.
  15. */
  16. const server = http.createServer(app);
  17. /**
  18. * Listen on provided port, on all network interfaces.
  19. */
  20. server.listen(port);
  21. server.on('error', onError);
  22. server.on('listening', onListening);
  23. /**
  24. * Normalize a port into a number, string, or false.
  25. */
  26. function normalizePort (val) {
  27. const port = parseInt(val, 10);
  28. if (isNaN(port)) {
  29. // named pipe
  30. return val;
  31. }
  32. if (port >= 0) {
  33. // port number
  34. return port;
  35. }
  36. return false;
  37. }
  38. /**
  39. * Event listener for HTTP server "error" event.
  40. */
  41. function onError (error) {
  42. if (error.syscall !== 'listen') {
  43. throw error;
  44. }
  45. const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
  46. // handle specific listen errors with friendly messages
  47. switch (error.code) {
  48. case 'EACCES':
  49. console.error(`${bind} requires elevated privileges`);
  50. process.exit(1);
  51. break;
  52. case 'EADDRINUSE':
  53. console.error(`${bind} is already in use`);
  54. process.exit(1);
  55. break;
  56. default:
  57. throw error;
  58. }
  59. }
  60. /**
  61. * Event listener for HTTP server "listening" event.
  62. */
  63. function onListening () {
  64. const addr = server.address();
  65. const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
  66. debug(`Listening on ${bind}`);
  67. }