www.js 1.6 KB

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