feat(views): zero-config static directory serving
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+130
-1
No files matched your search
@@ -69,6 +69,18 @@ export async function handle(
|
||||
const matched = matchRoute(routes, req.method, path);
|
||||
|
||||
if (!matched) {
|
||||
if ((req.method === 'GET' || req.method === 'HEAD') && opts?.static) {
|
||||
const hit = await opts.static(path);
|
||||
|
||||
if (hit) {
|
||||
const body = req.method === 'HEAD' ? '' : hit.body;
|
||||
return {
|
||||
injected,
|
||||
outcome: { kind: 'buffered', status: 200, headers: { 'content-type': hit.contentType }, body },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Path matches a route under a different method → 405 with Allow; otherwise 404.
|
||||
const allow = allowedMethods(routes, path);
|
||||
const err = allow.length ? new HttpError(405, 'Method Not Allowed') : new NotFound('Not Found');
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { PipelineResult } from '../pipeline';
|
||||
import type { ErrorRenderer } from '../transformers';
|
||||
import type { TlsOptions, SecurityOptions, CorsOptions } from '../security';
|
||||
import type { WsRequest } from './ws-core';
|
||||
import type { StaticResolver } from '../views';
|
||||
|
||||
/** Per-request size and timeout ceilings applied by the server. */
|
||||
export interface RequestLimits {
|
||||
@@ -24,6 +25,7 @@ export interface HttpOptions {
|
||||
cors?: CorsOptions;
|
||||
bodyDuplicates?: 'array' | 'last';
|
||||
onError?: ErrorRenderer;
|
||||
static?: StaticResolver;
|
||||
}
|
||||
|
||||
/** Handler for a matched HTTP route; receives a normalized request and resolves to a pipeline result. */
|
||||
|
||||
Reference in new issue
Block a user