Advertisement
Component

Node.js startup pattern

Mar 16th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright (c) TySoft Limited 2014 - ALL RIGHTS RESERVED.
  3.  */
  4.  
  5. 'use strict';
  6.  
  7. var Url = require('url'),
  8.     Async = require('async'),
  9.     Express = require('express'),
  10.     Passport = require('passport'),
  11.     ExpressApp = Express(),
  12.     Connect = require('connect'),
  13.     MongoStore = require('connect-mongo')(Express),
  14.     HttpServer = require('http').createServer(ExpressApp),
  15.     Config = require('./config/config'),
  16.     DatabaseConfig = require('./config/config-database');
  17.  
  18. var session_store = null;
  19.  
  20. Async.waterfall([
  21.     // Environment configuration
  22.     function (done) {
  23.         done(Config.checkConfig());
  24.     },
  25.     // Connect to the database
  26.     function (done) {
  27.         DatabaseConfig.configDatabase(done);
  28.     },
  29.     // Configure express
  30.     function (done) {
  31.  
  32.         // Store sessions in MongoDB
  33.         session_store = new MongoStore({
  34.             host: DatabaseConfig.database.host,
  35.             port: DatabaseConfig.database.port,
  36.             db: DatabaseConfig.database.name,
  37.             username: DatabaseConfig.database.username,
  38.             password: DatabaseConfig.database.password
  39.         });
  40.  
  41.         // Configure a public static content server
  42.         ExpressApp.use(Express.static(__dirname + '/client' + (Config.inProduction() ? '-build' : '')));
  43.  
  44.         // Use compress to automatically gzip requests and responses
  45.         ExpressApp.use(Express.compress());
  46.  
  47.         // Enable cookie parsing, needed for our sessions
  48.         ExpressApp.use(Express.cookieParser());
  49.  
  50.         // Enable body parsing so we can access the request body as an object
  51.         ExpressApp.use(Connect.urlencoded());
  52.         ExpressApp.use(Connect.json());
  53.  
  54.         // Make sure we set the session cookie on subdomains in prod so it can be accessed on the socket domain
  55.         var cookie_domain = null;
  56.         if (Config.inProduction()) {
  57.             var server_url = Url.parse(Config.getConfig(Config.keys.server_url)),
  58.                 parts = server_url.hostname.split('.');
  59.             cookie_domain = '.' + parts[parts.length - 2] + '.' + parts[parts.length - 1];
  60.         }
  61.  
  62.         // Enable sessions
  63.         ExpressApp.use(Express.session({
  64.             cookie: { domain: cookie_domain},
  65.             key: Config.getConfig(Config.keys.session_key),
  66.             secret: Config.getConfig(Config.keys.session_secret),
  67.             store: session_store
  68.         }));
  69.  
  70.         // Use passport for authentication
  71.         ExpressApp.use(Passport.initialize());
  72.         ExpressApp.use(Passport.session());
  73.  
  74.         // Development specific configuration
  75.         ExpressApp.configure('development', function () {
  76.             ExpressApp.use(Express.errorHandler({
  77.                 dumpExceptions: true,
  78.                 showStack: true
  79.             }));
  80.         });
  81.  
  82.         // Production specific configuration
  83.         ExpressApp.configure('production', function () {
  84.             ExpressApp.use(Express.errorHandler());
  85.         });
  86.  
  87.         done();
  88.     },
  89.     // Configure passport
  90.     function (done) {
  91.         require('./config/config-authentication').config(ExpressApp, done);
  92.     },
  93.     // Configure socket.io
  94.     function (done) {
  95.         var SocketConfig = require('./config/config-sockets');
  96.         SocketConfig.configureSockets(HttpServer, session_store);
  97.         done();
  98.     },
  99.     // Start express server
  100.     function (done) {
  101.  
  102.         var server_listening = false;
  103.  
  104.         HttpServer.once('error', function (err) {
  105.             if (!server_listening) {
  106.                 done(err);
  107.             }
  108.         });
  109.  
  110.         HttpServer.once('listening', function () {
  111.             server_listening = true;
  112.             done();
  113.         });
  114.  
  115.         HttpServer.listen(Config.getConfig(Config.keys.port));
  116.     },
  117.     // Load http controllers
  118.     function (done) {
  119.         require('./authentication/authentication-controller')(ExpressApp);
  120.         require('./users/user-controller')(ExpressApp);
  121.         require('./games/game-controller')(ExpressApp);
  122.         require('./stories/story-controller')(ExpressApp);
  123.         done();
  124.     }],
  125.     // Handle any errors encountered during startup
  126.     function (err) {
  127.         if (!err) {
  128.             console.log('Poker server startup complete.');
  129.         } else {
  130.             console.log('Something went wrong during server startup. Exiting.');
  131.             throw err;
  132.         }
  133.     }
  134. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement