A minimal static file handler for chubbyts-http-types.
- node: 16
- @chubbyts/chubbyts-http-error: ^2.3.1
- @chubbyts/chubbyts-http-types: ^1.2.3
Through NPM as @chubbyts/chubbyts-http-static-file.
npm i @chubbyts/chubbyts-http-static-file@^2.0.1
import { createStaticFileHandler } from '@chubbyts/chubbyts-http-static-file/dist/handler';
import type { ResponseFactory, StreamFromFileFactory } from '@chubbyts/chubbyts-http-types/dist/message-factory';
import { createGetRoute } from '@chubbyts/chubbyts-framework/dist/router/route';
const responseFactory: ResponseFactory = ...;
const streamFromFileFactory: StreamFromFileFactory = ...;
const handler = createStaticFileHandler(
responseFactory,
streamFromFileFactory,
'/path/to/public/directory',
(await import('../src/mimetypes')).default, // typescript / ecmascript module
// require('../src/mimetypes').default, // commonjs (cjs)
);
// for example as a fallback route matching everything
const route = createGetRoute({
path: '/(.*)',
name: 'static_file',
handler,
});
2024 Dominik Zogg