app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * 应用程序入口。
  3. */
  4. "use strict";
  5. let express = require('express');
  6. let path = require('path');
  7. let favicon = require('serve-favicon');
  8. let logger = require('morgan');
  9. let cookieParser = require('cookie-parser');
  10. let bodyParser = require('body-parser');
  11. let log = require('./util/log');
  12. let redis = require('redis');
  13. let methodOverride = require('method-override');
  14. // server configurations
  15. let configFile = require('./include/commons').CONFIG_FILE;
  16. let config = require('./resources/config/' + configFile);
  17. // handlers
  18. let SocketHandler = require('./handlers/socket.handler');
  19. let UrlInitializer = require('./endpoints/url.initializer');
  20. // initialize express application
  21. let app = express();
  22. app.set('port', config.serverPort);
  23. // view engine
  24. app.set('views', path.join(__dirname, 'views'));
  25. app.set('view engine', 'jade');
  26. // logger, body parser, cookie parser and view path
  27. app.use(favicon(__dirname + '/public/favicon.ico', null));
  28. app.use(logger('dev'));
  29. app.use(bodyParser.json());
  30. app.use(bodyParser.urlencoded({extended: true}));
  31. app.use(cookieParser());
  32. app.use(express.static(path.join(__dirname, 'public')));
  33. app.use(methodOverride(null, {methods: ['GET', 'POST']}));
  34. // web pages and endpoint
  35. UrlInitializer.initWebPages(app);
  36. UrlInitializer.initRestApi(app);
  37. // error handler, only handle the sync call exception
  38. app.use(function (err, req, res, next) {
  39. if (err) {
  40. res
  41. .status(err.httpStatus || 500)
  42. .send({message: err.message});
  43. }
  44. next(err);
  45. });
  46. // In development mode, error handler will print stacktrace, only handle the sync call exception
  47. if (app.get('env') === 'development') {
  48. app.use(function (err, req, res, next) {
  49. log.error(err);
  50. next(err);
  51. });
  52. }
  53. // only handle the sync call exception
  54. process.on('uncaughtException', function (err) {
  55. console.error(err);
  56. });
  57. // now enable http server and socket.io
  58. let server = require('http').createServer(app);
  59. let io = require('socket.io').listen(server);
  60. server.listen(config.serverPort);
  61. // event listener for HTTP server "error" event
  62. server.on('error', function (error) {
  63. if (error.syscall !== 'listen') throw error;
  64. let bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
  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. // event listener for HTTP server "listening" event
  80. server.on('listening', function onListening() {
  81. let addr = server.address();
  82. let bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
  83. });
  84. let socketHandler = new SocketHandler(io);
  85. socketHandler.start();
  86. log.info('Starting IM server, version ' + config.version + ', running on port ' + server.address().port + ', ' + new Date().toLocaleString());
  87. log.info('Configuration profile: ' + configFile.split('.')[1]);