Created
December 12, 2023 22:01
-
-
Save fgp555/9e45fe6a7f54b911c54de8af7d2bb748 to your computer and use it in GitHub Desktop.
11_express_ssg_jonmircha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from "express"; | |
import fs from "fs/promises"; | |
import path from "path"; | |
import markdownIt from "markdown-it"; | |
import fm from "front-matter"; | |
import cookieParser from "cookie-parser"; | |
import morgan from "morgan"; | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
// const __dirname = path.dirname(new URL(import.meta.url).pathname); | |
import { fileURLToPath } from "url"; | |
const __dirname = fileURLToPath(new URL(".", import.meta.url)); | |
app.use(morgan("dev")); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.set("views", path.join(__dirname, "pages")); | |
app.set("view engine", "pug"); | |
app.use(express.static(path.join(__dirname, "public"))); | |
// Rutas dinámicas desde archivos en la carpeta "pages" | |
const pagesDir = path.join(__dirname, "pages"); | |
const files = await fs.readdir(pagesDir); | |
// Aquí lógica para archivos html y md | |
for (let file of files) { | |
const filePath = path.join(pagesDir, file); | |
let extname = path.extname(file); | |
console.log(file, filePath, extname); | |
if (extname === ".md" || extname === ".pug" || extname === ".html") { | |
let fileName = path.basename(file, extname); | |
console.log(fileName); | |
app.get(`/${fileName}`, async (req, res) => { | |
try { | |
if (extname === ".pug") { | |
res.render(fileName); | |
} | |
if (extname === ".html") { | |
res.sendFile(filePath); | |
} | |
if (extname === ".md") { | |
let fileContent = await fs.readFile(filePath, "utf-8"); | |
let { attributes: frontMatterAttributes, body } = fm(fileContent); | |
let attributes = frontMatterAttributes; | |
let contentHTML = markdownIt().render(body); | |
res.render("layout-markdown", { ...attributes, contentHTML }); | |
} | |
} catch (err) { | |
res.status(404).render("error-404"); | |
} | |
}); | |
} | |
} | |
//Ruta de la página principal | |
app.get("/", (req, res) => { | |
res.render("index"); | |
}); | |
//Ruta del error 404 | |
app.use((req, res) => { | |
res.status(404).render("error-404"); | |
}); | |
app.listen(port, () => | |
console.log(`Sitio web corriendo en http://localhost:${port}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment