• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
peterqliu

peterqliu

static

Public
Like
static
Home
Code
2
README.md
H
server.ts
Branches
1
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
Code
/
Search
server.ts
https://peterqliu--ecd8a048ccab11f0909042dde27851f2.web.val.run
README.md

Importable Static File Server

This Val provides a reusable static file server that can be imported into other Val Town projects to serve static assets from the importing project's directory structure.

Features

  • Serves static files (HTML, CSS, JS, images, etc.) from the importing project
  • Intelligent relative path resolution based on referer headers
  • Automatic index.html serving for directory requests
  • Support for nested directory structures
  • Backward compatible with existing usage

Usage

As an Importable Library

Import the createStaticServer function into your HTTP val:

import { createStaticServer } from "https://esm.town/v/peterqliu/static/server.ts"; // Create a static server that serves files from your project const staticServer = createStaticServer(import.meta.url); export default async function (req: Request): Promise<Response> { return staticServer(req); }

With Hono Framework

import { Hono } from "https://esm.sh/hono@3.11.7"; import { createStaticServer } from "https://esm.town/v/peterqliu/static/server.ts"; const app = new Hono(); const staticServer = createStaticServer(import.meta.url); // Serve API routes app.get("/api/hello", (c) => c.json({ message: "Hello World" })); // Serve static files for all other routes app.get("*", async (c) => { return staticServer(c.req.raw); }); export default app.fetch;

How It Works

  1. Path Resolution: The server analyzes the importing project's import.meta.url to determine the correct base directory
  2. Relative Asset Detection: Uses referer headers to intelligently resolve relative paths (e.g., ./style.css from /subdir/page.html)
  3. Fallback Logic: Tries multiple resolution strategies and falls back to serving index.html for directory requests
  4. File Type Detection: Recognizes common web asset extensions for proper handling

API Reference

createStaticServer(importMetaUrl: string)

Creates a static file server function.

Parameters:

  • importMetaUrl: The import.meta.url from your importing project

Returns:

  • (req: Request) => Promise<Response>: A function that handles HTTP requests
HTTP
  • server.ts
    peterqliu--ec…f2.web.val.run
Code
README.md
H
server.ts
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.