src/server/index.js
function createServer(config: { configPath: ?string, configOptions: Object }) {
  const appHandler = express();
  appHandler.disable('etag');

  const { configPath, configOptions } = config;

  const compiler = new Compiler({
    configPath,
    configOptions,
  });

  const loggerMiddleware = initUI(compiler, configOptions);

  process.on('uncaughtException', err => {
    compiler.terminate();
    throw err;
  });

  process.on('SIGINT', () => {
    compiler.terminate();
    process.exit(0);
  });

  process.on('SIGTERM', () => {
    compiler.terminate();
    process.exit(2);
  });

  const compilerMiddleware = createCompilerMiddleware(compiler, {
    configPath,
    configOptions,
  });

  const httpServer = http.createServer(appHandler);

  const webSocketServer = new WebSocketServer({ server: httpServer });
  const debuggerProxy = new WebSocketDebuggerProxy(
    webSocketProxy(webSocketServer, '/debugger-proxy')
  );

  if (!configOptions.disableHotReloading) {
    hotMiddleware(compiler, {
      nativeProxy: webSocketProxy(webSocketServer, '/hot'),
      haulProxy: webSocketProxy(webSocketServer, '/haul-hmr'),
    });
  }

  // Middlewares
  appHandler
    .use(express.static(path.join(__dirname, '/assets/public')))
    .use(rawBodyMiddleware)
    .use(cleanPathMiddleware)
    .use(devToolsMiddleware(debuggerProxy))
    .use(liveReloadMiddleware(compiler))
    .use(statusPageMiddleware)
    .use(
      symbolicateMiddleware(compiler, {
        configPath,
        configOptions,
      })
    )
    .use(openInEditorMiddleware())
    .use('/systrace', systraceMiddleware)
    .use(loggerMiddleware)
    .use(compilerMiddleware)
    .use(missingBundleMiddleware);

  if (configOptions.eager) {
    if (
      !Array.isArray(configOptions.eager) &&
      typeof configOptions.eager === 'boolean'
    ) {
      // implicitly true...
      // TODO: Eager loading for all platforms
      configOptions.eager = ['android', 'ios' /* , ... potentially more? */];
    }

    configOptions.eager.forEach(platform => {
      compiler.emit(Compiler.Events.REQUEST_BUNDLE, {
        filename: `/index.${platform}.bundle`, // XXX: maybe the entry bundle is arbitary
        platform,
        callback() {},
      });
    });
  }

  return httpServer;
}
function createServer(config: { configPath: ?string, configOptions: Object }) {
  const appHandler = express();
  appHandler.disable('etag');

  const { configPath, configOptions } = config;

  const compiler = new Compiler({
    configPath,
    configOptions,
  });

  const loggerMiddleware = initUI(compiler, configOptions);

  process.on('uncaughtException', err => {
    compiler.terminate();
    throw err;
  });

  process.on('SIGINT', () => {
    compiler.terminate();
    process.exit(0);
  });

  process.on('SIGTERM', () => {
    compiler.terminate();
    process.exit(2);
  });

  const compilerMiddleware = createCompilerMiddleware(compiler, {
    configPath,
    configOptions,
  });

  const httpServer = http.createServer(appHandler);

  const webSocketServer = new WebSocketServer({ server: httpServer });
  const debuggerProxy = new WebSocketDebuggerProxy(
    webSocketProxy(webSocketServer, '/debugger-proxy')
  );

  if (!configOptions.disableHotReloading) {
    hotMiddleware(compiler, {
      nativeProxy: webSocketProxy(webSocketServer, '/hot'),
      haulProxy: webSocketProxy(webSocketServer, '/haul-hmr'),
    });
  }

  // Middlewares
  appHandler
    .use(express.static(path.join(__dirname, '/assets/public')))
    .use(rawBodyMiddleware)
    .use(cleanPathMiddleware)
    .use(devToolsMiddleware(debuggerProxy))
    .use(liveReloadMiddleware(compiler))
    .use(statusPageMiddleware)
    .use(
      symbolicateMiddleware(compiler, {
        configPath,
        configOptions,
      })
    )
    .use(openInEditorMiddleware())
    .use('/systrace', systraceMiddleware)
    .use(loggerMiddleware)
    .use(compilerMiddleware)
    .use(missingBundleMiddleware);

  if (configOptions.eager) {
    if (
      !Array.isArray(configOptions.eager) &&
      typeof configOptions.eager === 'boolean'
    ) {
      // implicitly true...
      // TODO: Eager loading for all platforms
      configOptions.eager = ['android', 'ios' /* , ... potentially more? */];
    }

    configOptions.eager.forEach(platform => {
      compiler.emit(Compiler.Events.REQUEST_BUNDLE, {
        filename: `/index.${platform}.bundle`, // XXX: maybe the entry bundle is arbitary
        platform,
        callback() {},
      });
    });
  }

  return httpServer;
}
Packager-like Server running on top of Webpack