import express from "express"; import { createServer as createViteServer } from "vite"; import fs from "fs/promises"; import path from "path"; import { fileURLToPath } from "url"; import { BLOG_POSTS } from "./src/lib/data.ts"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); async function startServer() { const app = express(); const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3000; let vite: any; if (process.env.NODE_ENV !== "production") { vite = await createViteServer({ server: { middlewareMode: true }, appType: "custom", }); app.use(vite.middlewares); } else { // Serve static files from dist/assets and others, but not index.html directly app.use(express.static(path.join(process.cwd(), "dist"), { index: false })); } app.get("/sitemap.xml", (req, res) => { const baseUrl = process.env.APP_URL || "https://helenacosta.dev"; let xml = ` ${baseUrl}/weekly1.0 ${baseUrl}/blogdaily0.9 ${baseUrl}/curriculomonthly0.8 ${baseUrl}/contatomonthly0.8`; BLOG_POSTS.forEach((post) => { xml += ` ${baseUrl}/blog/${post.slug}monthly0.7`; }); xml += ` `; res.header("Content-Type", "application/xml"); res.send(xml); }); app.get("/robots.txt", (req, res) => { const baseUrl = process.env.APP_URL || "https://helenacosta.dev"; const txt = `User-agent: * Allow: / Sitemap: ${baseUrl}/sitemap.xml`; res.header("Content-Type", "text/plain"); res.send(txt); }); app.get("*", async (req, res) => { try { let template, render; if (process.env.NODE_ENV !== "production") { template = await fs.readFile(path.join(__dirname, "index.html"), "utf-8"); template = await vite.transformIndexHtml(req.originalUrl, template); } else { template = await fs.readFile(path.join(process.cwd(), "dist", "index.html"), "utf-8"); } // Default tags let title = "Helena Costa | Front-end Engineer & Designer"; let description = "Engenheira de Software & Designer de Interfaces. Construindo experiências web de alta performance."; let image = "https://images.unsplash.com/photo-1544725176-7c40e5a71c5e?auto=format&fit=crop&q=80&w=1200&h=630"; const appUrl = process.env.APP_URL || "https://helenacosta.dev"; // If viewing a post if (req.originalUrl.startsWith("/blog/")) { const slug = req.originalUrl.split("/")[2]; const post = BLOG_POSTS.find(p => p.slug === slug); if (post) { title = `${post.title} | Helena Costa`; description = post.excerpt; if (post.coverImage) { image = post.coverImage; } } } // Replace generic meta tags in index.html with specific ones let html = template .replace(/.*<\/title>/i, `<title>${title}`) .replace( //, ` ` ); res.status(200).set({ "Content-Type": "text/html" }).end(html); } catch (e) { if (process.env.NODE_ENV !== "production") { vite.ssrFixStacktrace(e as Error); } console.error(e); res.status(500).end((e as Error).message); } }); app.listen(PORT, "0.0.0.0", () => { console.log(`Server running on http://0.0.0.0:${PORT}`); }); } startServer();