Skip to content

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.

  • source is a FileInterface or a FolderInterface.
  • path exposes a single file on a specific route.
  • prefix exposes a whole folder under a URL prefix.
  • cacheControlConfig adds cache-control headers.
  • directoryFallBackFile serves a default file for a folder, for example index.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.

  • source is the file to serve.
  • path is the HTTP route to expose.
  • cacheControlConfig is 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.

  • source is the root folder.
  • prefix is the URL prefix used to resolve files.
  • directoryFallBackFile serves a default file for a folder, for example index.html.
  • cacheControlConfig is optional.

Released under the MIT License.