|
| 1 | +// server/index.ts |
| 2 | +import express2 from "express"; |
| 3 | + |
| 4 | +// server/routes.ts |
| 5 | +import { createServer } from "http"; |
| 6 | +async function registerRoutes(app2) { |
| 7 | + const httpServer = createServer(app2); |
| 8 | + return httpServer; |
| 9 | +} |
| 10 | + |
| 11 | +// server/vite.ts |
| 12 | +import express from "express"; |
| 13 | +import fs from "fs"; |
| 14 | +import path2 from "path"; |
| 15 | +import { createServer as createViteServer, createLogger } from "vite"; |
| 16 | + |
| 17 | +// vite.config.ts |
| 18 | +import { defineConfig } from "vite"; |
| 19 | +import react from "@vitejs/plugin-react"; |
| 20 | +import path from "path"; |
| 21 | +var vite_config_default = defineConfig(({ command }) => ({ |
| 22 | + base: command === "serve" ? "/" : "/material-dashboard-shadcn/", |
| 23 | + plugins: [ |
| 24 | + react() |
| 25 | + ], |
| 26 | + resolve: { |
| 27 | + alias: { |
| 28 | + "@": path.resolve(import.meta.dirname, "client", "src"), |
| 29 | + "@shared": path.resolve(import.meta.dirname, "shared"), |
| 30 | + "@assets": path.resolve(import.meta.dirname, "attached_assets") |
| 31 | + } |
| 32 | + }, |
| 33 | + root: path.resolve(import.meta.dirname, "client"), |
| 34 | + build: { |
| 35 | + outDir: path.resolve(import.meta.dirname, "build"), |
| 36 | + emptyOutDir: true |
| 37 | + }, |
| 38 | + server: { |
| 39 | + fs: { |
| 40 | + strict: true, |
| 41 | + deny: ["**/.*"] |
| 42 | + } |
| 43 | + } |
| 44 | +})); |
| 45 | + |
| 46 | +// server/vite.ts |
| 47 | +import { nanoid } from "nanoid"; |
| 48 | +var viteLogger = createLogger(); |
| 49 | +function log(message, source = "express") { |
| 50 | + const formattedTime = (/* @__PURE__ */ new Date()).toLocaleTimeString("en-US", { |
| 51 | + hour: "numeric", |
| 52 | + minute: "2-digit", |
| 53 | + second: "2-digit", |
| 54 | + hour12: true |
| 55 | + }); |
| 56 | + console.log(`${formattedTime} [${source}] ${message}`); |
| 57 | +} |
| 58 | +async function setupVite(app2, server) { |
| 59 | + const serverOptions = { |
| 60 | + middlewareMode: true, |
| 61 | + hmr: { server }, |
| 62 | + allowedHosts: true |
| 63 | + }; |
| 64 | + const vite = await createViteServer({ |
| 65 | + ...vite_config_default, |
| 66 | + configFile: false, |
| 67 | + customLogger: { |
| 68 | + ...viteLogger, |
| 69 | + error: (msg, options) => { |
| 70 | + viteLogger.error(msg, options); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | + }, |
| 74 | + server: serverOptions, |
| 75 | + appType: "custom" |
| 76 | + }); |
| 77 | + app2.use(vite.middlewares); |
| 78 | + app2.use("*", async (req, res, next) => { |
| 79 | + const url = req.originalUrl; |
| 80 | + try { |
| 81 | + const clientTemplate = path2.resolve( |
| 82 | + import.meta.dirname, |
| 83 | + "..", |
| 84 | + "client", |
| 85 | + "index.html" |
| 86 | + ); |
| 87 | + let template = await fs.promises.readFile(clientTemplate, "utf-8"); |
| 88 | + template = template.replace( |
| 89 | + `src="/src/main.tsx"`, |
| 90 | + `src="/src/main.tsx?v=${nanoid()}"` |
| 91 | + ); |
| 92 | + const page = await vite.transformIndexHtml(url, template); |
| 93 | + res.status(200).set({ "Content-Type": "text/html" }).end(page); |
| 94 | + } catch (e) { |
| 95 | + vite.ssrFixStacktrace(e); |
| 96 | + next(e); |
| 97 | + } |
| 98 | + }); |
| 99 | +} |
| 100 | +function serveStatic(app2) { |
| 101 | + const distPath = path2.resolve(import.meta.dirname, "public"); |
| 102 | + if (!fs.existsSync(distPath)) { |
| 103 | + throw new Error( |
| 104 | + `Could not find the build directory: ${distPath}, make sure to build the client first` |
| 105 | + ); |
| 106 | + } |
| 107 | + app2.use(express.static(distPath)); |
| 108 | + app2.use("*", (_req, res) => { |
| 109 | + res.sendFile(path2.resolve(distPath, "index.html")); |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +// server/index.ts |
| 114 | +var app = express2(); |
| 115 | +app.use(express2.json()); |
| 116 | +app.use(express2.urlencoded({ extended: false })); |
| 117 | +app.use((req, res, next) => { |
| 118 | + const start = Date.now(); |
| 119 | + const path3 = req.path; |
| 120 | + let capturedJsonResponse = void 0; |
| 121 | + const originalResJson = res.json; |
| 122 | + res.json = function(bodyJson, ...args) { |
| 123 | + capturedJsonResponse = bodyJson; |
| 124 | + return originalResJson.apply(res, [bodyJson, ...args]); |
| 125 | + }; |
| 126 | + res.on("finish", () => { |
| 127 | + const duration = Date.now() - start; |
| 128 | + if (path3.startsWith("/api")) { |
| 129 | + let logLine = `${req.method} ${path3} ${res.statusCode} in ${duration}ms`; |
| 130 | + if (capturedJsonResponse) { |
| 131 | + logLine += ` :: ${JSON.stringify(capturedJsonResponse)}`; |
| 132 | + } |
| 133 | + if (logLine.length > 80) { |
| 134 | + logLine = logLine.slice(0, 79) + "\u2026"; |
| 135 | + } |
| 136 | + log(logLine); |
| 137 | + } |
| 138 | + }); |
| 139 | + next(); |
| 140 | +}); |
| 141 | +(async () => { |
| 142 | + const server = await registerRoutes(app); |
| 143 | + app.use((err, _req, res, _next) => { |
| 144 | + const status = err.status || err.statusCode || 500; |
| 145 | + const message = err.message || "Internal Server Error"; |
| 146 | + res.status(status).json({ message }); |
| 147 | + throw err; |
| 148 | + }); |
| 149 | + if (app.get("env") === "development") { |
| 150 | + await setupVite(app, server); |
| 151 | + } else { |
| 152 | + serveStatic(app); |
| 153 | + } |
| 154 | + const port = process.env.PORT ? parseInt(process.env.PORT) : 3e3; |
| 155 | + server.listen(port, "0.0.0.0", () => { |
| 156 | + log(`serving on port ${port}`); |
| 157 | + }); |
| 158 | +})(); |
0 commit comments