Node.js/Express-Server als Ersatz für Cloudflare Pages Functions + D1 (läuft auf eigenem Server statt Cloudflare)
Build & Deploy / deploy (push) Waiting to run
Build & Deploy / deploy (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/* =====================================================================
|
||||
index.js — Haupteinstiegspunkt. Baut den kompletten Express-Server auf: liefert die fertig
|
||||
gebaute Astro-Seite (../dist/) aus UND die API-Routen, genau wie Cloudflare Pages beides in
|
||||
einem Deployment vereint hat. Die Zugangscode-Schranke (middleware/gate.js) läuft vor JEDER
|
||||
Anfrage, exakt wie functions/_middleware.js das vorher bei Cloudflare tat.
|
||||
|
||||
Umgebungsvariablen kommen aus einer .env-Datei — gestartet wird dieser Server mit
|
||||
"node --env-file=.env index.js" (Node 22 kann .env-Dateien nativ laden, kein zusätzliches
|
||||
Paket nötig).
|
||||
===================================================================== */
|
||||
|
||||
import express from "express";
|
||||
import cookieParser from "cookie-parser";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { existsSync } from "node:fs";
|
||||
|
||||
import { initDb } from "./db.js";
|
||||
import { gateMiddleware } from "./middleware/gate.js";
|
||||
import { ordersRouter } from "./routes/orders.js";
|
||||
import { bestsellerRouter } from "./routes/bestseller.js";
|
||||
import { paypalRouter } from "./routes/paypal.js";
|
||||
import { authRouter } from "./routes/auth.js";
|
||||
import { accountRouter } from "./routes/account.js";
|
||||
import { verwaltungRouter } from "./routes/verwaltung.js";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const DIST_DIR = join(__dirname, "..", "dist");
|
||||
const PORT = Number(process.env.PORT || 4000);
|
||||
|
||||
initDb();
|
||||
|
||||
const app = express();
|
||||
|
||||
// Läuft hinter Caddy (Reverse Proxy) — nötig, damit req.protocol/req.get("host") und
|
||||
// "secure"-Cookies korrekt funktionieren, statt anzunehmen, die Verbindung sei unverschlüsselt.
|
||||
app.set("trust proxy", 1);
|
||||
|
||||
app.use(cookieParser());
|
||||
app.use(express.json({ limit: "2mb" }));
|
||||
|
||||
// Zugangscode-Schranke vor allem anderen — exakt wie functions/_middleware.js bei Cloudflare.
|
||||
app.use(gateMiddleware);
|
||||
|
||||
app.use(ordersRouter);
|
||||
app.use(bestsellerRouter);
|
||||
app.use(paypalRouter);
|
||||
app.use(authRouter);
|
||||
app.use(accountRouter);
|
||||
app.use(verwaltungRouter);
|
||||
|
||||
// Statische Auslieferung der fertig gebauten Astro-Seite (nach "npm run build" in ../).
|
||||
if (existsSync(DIST_DIR)) {
|
||||
app.use(express.static(DIST_DIR, { extensions: ["html"] }));
|
||||
} else {
|
||||
console.warn(`⚠️ ${DIST_DIR} existiert nicht — "npm run build" im Astro-Projekt (../) noch nicht ausgeführt.`);
|
||||
}
|
||||
|
||||
// 404 für alles, was weder Datei noch bekannte Route ist.
|
||||
app.use((req, res) => {
|
||||
res.status(404).sendFile(join(DIST_DIR, "404.html"), (err) => {
|
||||
if (err) res.status(404).send("Nicht gefunden.");
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(PORT, "127.0.0.1", () => {
|
||||
console.log(`Van's DIY & Bastelbedarf – Server läuft auf http://127.0.0.1:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user