Create a static entry point
@duplojs/http/static lets you expose a file or a folder in read-only mode. You can either plug staticPlugin directly into the Hub, or manually register a route with makeRouteFile or makeRouteFolder.
With staticPlugin
ts
import { createHub } from "@duplojs/http";
import { SF } from "@duplojs/server-utils";
import { staticPlugin } from "@duplojs/http/static";
const logoFile = SF.createFileInterface("./public/logo.svg");
const assetsFolder = SF.createFolderInterface("./public");
export const hub = createHub({ environment: "DEV" })
.plug(
staticPlugin(logoFile, {
path: "/logo",
cacheControlConfig: {
maxAge: 3600,
public: true,
},
}),
)
.plug(
staticPlugin(assetsFolder, {
prefix: "/assets",
}),
);The plugin automatically chooses the right behavior based on the source you pass.
sourceis aFileInterfaceor aFolderInterface.pathexposes a single file on a specific route.prefixexposes a whole folder under a URL prefix.cacheControlConfigaddscache-controlheaders.directoryFallBackFileserves a default file for a folder, for exampleindex.html.
With makeRouteFile
ts
import { createHub } from "@duplojs/http";
import { SF } from "@duplojs/server-utils";
import { makeRouteFile } from "@duplojs/http/static";
const faviconRoute = makeRouteFile({
source: SF.createFileInterface("./public/favicon.ico"),
path: "/favicon.ico",
cacheControlConfig: {
maxAge: 86400,
public: true,
},
});
export const hub = createHub({ environment: "DEV" })
.register(faviconRoute);Use this function when you want to register a static route yourself for a single file.
sourceis the file to serve.pathis the HTTP route to expose.cacheControlConfigis optional.
With makeRouteFolder
ts
import { createHub } from "@duplojs/http";
import { SF } from "@duplojs/server-utils";
import { makeRouteFolder } from "@duplojs/http/static";
const docsRoute = makeRouteFolder({
source: SF.createFolderInterface("./public/docs"),
prefix: "/docs",
directoryFallBackFile: "index.html",
cacheControlConfig: {
maxAge: 600,
public: true,
},
});
export const hub = createHub({ environment: "DEV" })
.register(docsRoute);This version serves every file in a folder from a shared prefix.
sourceis the root folder.prefixis the URL prefix used to resolve files.directoryFallBackFileserves a default file for a folder, for exampleindex.html.cacheControlConfigis optional.
