Replit-Commit-Author: Agent Replit-Commit-Session-Id: 0aa507c2-4fa6-42bc-9ce7-ee6f559c10a5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/9dda30b6-4149-4bce-89dc-76333005952c/3ad1e152-cd06-4cd1-871a-477ec6981df3.jpg
103 lines
3.1 KiB
TypeScript
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);
|
|
}); |