brackt/plans/phase-3-server-js-changes.md

3.3 KiB

Phase 3: Exact Changes to server.js

Current server.js (lines 1-10)

import compression from "compression";
import express from "express";
import morgan from "morgan";

// Short-circuit the type-checking of the built output.
const BUILD_PATH = "./build/server/index.js";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PORT = Number.parseInt(process.env.PORT || "3000");

const app = express();

Add at top (after imports)

import compression from "compression";
import express from "express";
import morgan from "morgan";
import { createServer } from "http";  // ← ADD THIS LINE

// Short-circuit the type-checking of the built output.
const BUILD_PATH = "./build/server/index.js";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PORT = Number.parseInt(process.env.PORT || "3000");

const app = express();

Current server.js (end of file)

  app.use(await import(BUILD_PATH).then((mod) => mod.app));
}

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Replace with (end of file)

  app.use(await import(BUILD_PATH).then((mod) => mod.app));
}

// ← ADD THESE LINES ↓
const httpServer = createServer(app);

// Initialize Socket.IO
const { initializeSocketIO } = await import("./server/socket.js");
initializeSocketIO(httpServer);

httpServer.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});
// ← END OF ADDITIONS

Complete Diff

 import compression from "compression";
 import express from "express";
 import morgan from "morgan";
+import { createServer } from "http";

 // Short-circuit the type-checking of the built output.
 const BUILD_PATH = "./build/server/index.js";
 const DEVELOPMENT = process.env.NODE_ENV === "development";
 const PORT = Number.parseInt(process.env.PORT || "3000");

 const app = express();

 app.use(compression());
 app.disable("x-powered-by");

 if (DEVELOPMENT) {
   console.log("Starting development server");
   const viteDevServer = await import("vite").then((vite) =>
     vite.createServer({
       server: { middlewareMode: true },
     }),
   );
   app.use(viteDevServer.middlewares);
   app.use(async (req, res, next) => {
     try {
       const source = await viteDevServer.ssrLoadModule("./server/app.ts");
       return await source.app(req, res, next);
     } catch (error) {
       if (typeof error === "object" && error instanceof Error) {
         viteDevServer.ssrFixStacktrace(error);
       }
       next(error);
     }
   });
 } else {
   console.log("Starting production server");
   app.use(
     "/assets",
     express.static("build/client/assets", { immutable: true, maxAge: "1y" }),
   );
   app.use(morgan("tiny"));
   app.use(express.static("build/client", { maxAge: "1h" }));
   app.use(await import(BUILD_PATH).then((mod) => mod.app));
 }

-app.listen(PORT, () => {
+const httpServer = createServer(app);
+
+// Initialize Socket.IO
+const { initializeSocketIO } = await import("./server/socket.js");
+initializeSocketIO(httpServer);
+
+httpServer.listen(PORT, () => {
   console.log(`Server is running on http://localhost:${PORT}`);
 });

Summary

Lines Added: 5 Lines Changed: 1 Total Changes: 6 lines

This is the absolute minimum change needed to support Socket.IO while keeping the existing architecture intact.