@amirprestreshi/route-dh@1.0.7
A straightforward routing and static file handler for Deno.
route-dh
A straightforward routing and static file handler for Deno.
Easily define API endpoints, serve static assets, apply cache control, and customize error responses—all with plain JavaScript and only one dependency: Deno’s standard path module.
Features
- Clean API route handling (GET, POST, PUT, PATCH, DELETE)
- Static file serving with customizable folder structure
- Per-folder Cache-Control settings
- Custom status page handling for 400, 401, 403, 404, and 500
##Installation
deno add jsr:@amirprestreshi/route-dh
Import
import { DH_defineAssetsPaths, DH_defineStatusPagesPaths, DH_defineCacheControlPaths, DH_defineRoutes, DH_requestHandler } from "@amirprestreshi/route-dh";
Usage
- Define Asset
DH_defineAssetsPaths({ route: Deno.cwd(), // Required - define path directory of your server client: "./client", // Required - define path directory for your client project images: "./assets/images", // Optional scripts: "./assets/scripts", // Optional medias: "./assets/medias", // Optional documents: "./assets/docs", // Optional fonts: "./assets/fonts", // Optional wasm: "./assets/wasm" // Optional (for WebAssembly) });
- Define Custom Error Pages (Optional)
DH_defineStatusPagesPaths({ 400: "./errors/404.html", // Optional - define path for your HTML/image file, by default returns text/plain 401: "./errors/404.html", 403: "./errors/404.html", 404: "./errors/404.html", 500: "./errors/500.html" });
- Define Cache-Control Headers (Optional)
DH_defineCacheControlPaths({ images: "public, max-age=86400", // Optional - define cache-control for images scripts: "private, max-age=3600", // Optional medias: "private, max-age=3600", // Optional documents: "private, max-age=3600", // Optional fonts: "private, max-age=3600" // Optional });
- Define API Routes
DH_defineRoutes() .get("/api/hello", (req) => { return new Response("Hello from GET"); }) .post("/api/data", async (req) => { const data = await req.json(); return new Response(JSON.stringify({ received: data }), { headers: { "Content-Type": "application/json" } }); });
- Handle Requests
Deno.serve({ port: 5000 }, async (req) => { try { return DH_requestHandler(req, domainName); } catch (e) { return new Response("500 Internal Server Error", { status: 500, headers: { "Content-Type": "text/plain" } }); } });
Behavior
Static assets: Served from defined folders automatically.
API routing: Handled via DH_defineRoutes, no middleware—your handler function gets full control.
Error pages: Return plain text unless a file path is provided.
Cache-Control: Applied per folder; if not set, no cache headers are added.
Dependency
Uses only the path module from the Deno standard library.
License: MIT
Add Package
deno add jsr:@amirprestreshi/route-dh
Import symbol
import * as route_dh from "@amirprestreshi/route-dh";
Import directly with a jsr specifier
import * as route_dh from "jsr:@amirprestreshi/route-dh";