Pessoal/Template-02/server.ts

128 lines
4.5 KiB
TypeScript
Raw Normal View History

2026-05-13 22:21:40 +00:00
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 = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>${baseUrl}/</loc><changefreq>weekly</changefreq><priority>1.0</priority></url>
<url><loc>${baseUrl}/blog</loc><changefreq>daily</changefreq><priority>0.9</priority></url>
<url><loc>${baseUrl}/curriculo</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
<url><loc>${baseUrl}/contato</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>`;
BLOG_POSTS.forEach((post) => {
xml += `
<url><loc>${baseUrl}/blog/${post.slug}</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>`;
});
xml += `
</urlset>`;
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>.*<\/title>/i, `<title>${title}</title>`)
.replace(
/<!-- INJECT_SEO -->/,
`
<meta name="description" content="${description}" />
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website" />
<meta property="og:url" content="${appUrl}${req.originalUrl}" />
<meta property="og:title" content="${title}" />
<meta property="og:description" content="${description}" />
<meta property="og:image" content="${image}" />
<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content="${appUrl}${req.originalUrl}" />
<meta property="twitter:title" content="${title}" />
<meta property="twitter:description" content="${description}" />
<meta property="twitter:image" content="${image}" />
`
);
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();