Files
DowntownerArchive/server/index.ts

103 lines
3.1 KiB
TypeScript

import express, { type Request, Response, NextFunction } from "express";
import { registerRoutes } from "./routes";
import { setupVite, serveStatic, log } from "./vite";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use((req, res, next) => {
const start = Date.now();
const path = req.path;
let capturedJsonResponse: Record<string, any> | undefined = undefined;
const originalResJson = res.json;
res.json = function (bodyJson, ...args) {
capturedJsonResponse = bodyJson;
return originalResJson.apply(res, [bodyJson, ...args]);
};
res.on("finish", () => {
const duration = Date.now() - start;
if (path.startsWith("/api")) {
let logLine = `${req.method} ${path} ${res.statusCode} in ${duration}ms`;
if (capturedJsonResponse) {
logLine += ` :: ${JSON.stringify(capturedJsonResponse)}`;
}
if (logLine.length > 80) {
logLine = logLine.slice(0, 79) + "…";
}
log(logLine);
}
});
next();
});
// Function to start the server with fallback ports
const startServer = async (initialPort: number, maxRetries = 3) => {
let currentPort = initialPort;
let retries = 0;
while (retries < maxRetries) {
try {
const server = await registerRoutes(app);
app.use((err: any, _req: Request, res: Response, _next: NextFunction) => {
const status = err.status || err.statusCode || 500;
const message = err.message || "Internal Server Error";
res.status(status).json({ message });
throw err;
});
// importantly only setup vite in development and after
// setting up all the other routes so the catch-all route
// doesn't interfere with the other routes
if (app.get("env") === "development") {
await setupVite(app, server);
} else {
serveStatic(app);
}
// Start the server on the current port
return new Promise<void>((resolve, reject) => {
server.listen(currentPort, "0.0.0.0")
.on("error", (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
log(`Port ${currentPort} is already in use, trying another port...`);
currentPort++;
retries++;
if (retries >= maxRetries) {
reject(new Error(`Failed to find an available port after ${maxRetries} attempts`));
} else {
server.close();
}
} else {
reject(err);
}
})
.on("listening", () => {
log(`Server is running on port ${currentPort}`);
resolve();
});
});
} catch (error) {
log(`Error starting server: ${error instanceof Error ? error.message : String(error)}`);
retries++;
if (retries >= maxRetries) {
throw new Error(`Failed to start server after ${maxRetries} attempts`);
}
currentPort++;
}
}
};
// Start the server with port 5000 initially
startServer(5000)
.catch((error) => {
console.error("Failed to start server:", error);
process.exit(1);
});