Advertisement
Guest User

Untitled

a guest
May 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2.  
  3. /**
  4.  * Module dependencies.
  5.  */
  6.  
  7. var app = require('../app');
  8. var debug = require('debug')('forumforum:httpServer');
  9. var http = require('http');
  10.  
  11. /**
  12.  * Get port_public from environment and store in Express.
  13.  */
  14.  
  15. var port_public = normalizePort(process.env.PORT || '80');
  16. app.set('port_public', port_public);
  17.  
  18. /**
  19.  * Create HTTP httpServer.
  20.  */
  21.  
  22. var httpServer = http.createServer(app);
  23.  
  24. /**
  25.  * Listen on provided port_public, on all network interfaces.
  26.  */
  27.  
  28. httpServer.listen(port_public);
  29. httpServer.on('error', onError);
  30. httpServer.on('listening', onListening);
  31.  
  32. /**
  33.  * Normalize a port_public into a number, string, or false.
  34.  */
  35.  
  36. function normalizePort(val) {
  37.   var port_public = parseInt(val, 10);
  38.  
  39.   if (isNaN(port_public)) {
  40.     // named pipe
  41.     return val;
  42.   }
  43.  
  44.   if (port_public >= 0) {
  45.     // port_public number
  46.     return port_public;
  47.   }
  48.  
  49.   return false;
  50. }
  51.  
  52. /**
  53.  * Event listener for HTTP httpServer "error" event.
  54.  */
  55.  
  56. function onError(error) {
  57.   if (error.syscall !== 'listen') {
  58.     throw error;
  59.   }
  60.  
  61.   var bind = typeof port_public === 'string'
  62.     ? 'Pipe ' + port_public
  63.     : 'Port ' + port_public;
  64.  
  65.   // handle specific listen errors with friendly messages
  66.   switch (error.code) {
  67.     case 'EACCES':
  68.       console.error(bind + ' requires elevated privileges');
  69.       process.exit(1);
  70.       break;
  71.     case 'EADDRINUSE':
  72.       console.error(bind + ' is already in use');
  73.       process.exit(1);
  74.       break;
  75.     default:
  76.       throw error;
  77.   }
  78. }
  79.  
  80. /**
  81.  * Event listener for HTTP httpServer "listening" event.
  82.  */
  83.  
  84. function onListening() {
  85.   var addr = httpServer.address();
  86.   var bind = typeof addr === 'string'
  87.     ? 'pipe ' + addr
  88.     : 'port_public ' + addr.port_public;
  89.   debug('Listening on ' + bind);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement