www.js 1.6 KB

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