0.0.7 • Published 1 year ago
@biliblitz/node-server v0.0.7
@biliblitz/node-server
This package provides a static assets server.
import { serveStatic } from "@biliblitz/node-server";
// type = (req: Request) => Promise<Response | null>
const assets = serveStatic({ root: "./public" });
const response = await assets(new Request("https://localhost/path/to/file"));
// this will return file located in "./public/path/to/file" if exists.
And some tools for using your server from @biliblitz/blitz
.
import { chain, when, startsWith } from "@biliblitz/node-server/tools";
import server from "./dist/server/entry.server.js";
const public = serveStatic({ root: "./public/" });
const assets = serveStatic({ root: "./dist/client/" });
const fetch = chain(public, when(startsWith("/build/"), assets), server);
Tools:
chain
: run handlers one by onewhen
: run handler if condition is true
Conditions:
startsWith
: check if pathname of req.url starts with given valueendsWith
: same like abovematches
: check if req.pathname matches given regexmethod
: check if req.method is given valueand
: check if every given conditions is trueor
: check if some given condition is truenot
: check if given condition is false
Finally serve the server using @hono/node-server
.
import { serve } from "@hono/node-server";
const server: (req: Request) => Promise<Response>
= { /* ... */ };
serve({ fetch: server }, (info) => {
console.log(`Listening on http://localhost:${info.port}/`);
});