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

peterqliu

staticServer

write html/css/js like a local project
Public
Like
staticServer
Home
Code
9
examples
4
README.md
USAGE.md
animations.js
index.html
index.ts
script.js
H
server.ts
utils.js
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
/
USAGE.md
Code
/
USAGE.md
Search
…
Viewing readonly version of main branch: v40
View latest version
USAGE.md

How to Use the Exportable Static Server

This guide shows you how to import and use the static server in your own Val Town projects.

πŸš€ Quick Import

Method 1: Copy the Module

  1. Copy static-server.ts to your project
  2. Import and use:
import { quickStaticServer } from "./index.ts"; export default quickStaticServer();

Method 2: Direct Import (if public)

import { quickStaticServer } from "https://esm.town/v/[username]/[project]/static-server"; export default quickStaticServer();

πŸ“ Project Setup

Create this file structure in your Val Town project:

your-project/
β”œβ”€β”€ static-server.ts    # Copy this file
β”œβ”€β”€ server.ts           # Your HTTP val (imports static-server)
β”œβ”€β”€ index.html          # Your main HTML file
β”œβ”€β”€ script.js           # JavaScript files
β”œβ”€β”€ style.css           # CSS files
└── assets/
    β”œβ”€β”€ utils.js
    └── components.js

🎯 Usage Examples

1. Basic Static Server

File: server.ts

import { quickStaticServer } from "./static-server.ts"; // Serves index.html at root, all other files by path export default quickStaticServer();

Set this file as an HTTP trigger and you're done!

2. Custom Configuration

File: server.ts

import { createStaticServer } from "./static-server.ts"; const { handler } = createStaticServer({ indexFile: '/app.html', headers: { 'Cache-Control': 'max-age=3600', 'X-Powered-By': 'My Static Server' }, mimeTypes: { '.ts': 'text/typescript; charset=utf-8' } }); export default handler;

3. API + Static Files

File: server.ts

import { advancedStaticServer } from "./static-server.ts"; export default advancedStaticServer({ routes: { '/api/users': async () => { // Your API logic here return new Response(JSON.stringify([ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ]), { headers: { 'Content-Type': 'application/json' } }); }, '/api/health': () => new Response('OK') }, staticOptions: { indexFile: '/index.html' } });

4. Single Page Application

File: server.ts

import { createStaticServer } from "./static-server.ts"; const { handler, serveFile } = createStaticServer({ notFoundHandler: async (pathname) => { // For routes without extensions, serve the SPA if (!pathname.includes('.')) { return await serveFile('/index.html'); } return new Response('File not found', { status: 404 }); } }); export default handler;

πŸ”§ Configuration Options

Quick Setup Options

// Default index file quickStaticServer() // Custom index file quickStaticServer('/app.html')

Advanced Options

createStaticServer({ indexFile: '/index.html', // File served at root headers: { // Headers for all responses 'Cache-Control': 'no-cache' }, mimeTypes: { // Custom MIME types '.vue': 'text/vue; charset=utf-8' }, notFoundHandler: (pathname) => { // Custom 404 handler return new Response(`Not found: ${pathname}`, { status: 404 }); }, errorHandler: (error, pathname) => { // Custom error handler return new Response(`Error: ${error.message}`, { status: 500 }); } })

πŸ“ HTML File Setup

Your HTML files can reference any JS/CSS files:

File: index.html

<!DOCTYPE html> <html> <head> <title>My App</title> <link rel="stylesheet" href="/style.css"> <link rel="stylesheet" href="/components/header.css"> </head> <body> <div id="app">Loading...</div> <!-- All these will be served automatically --> <script src="/utils/helpers.js"></script> <script src="/components/header.js"></script> <script src="/app.js"></script> </body> </html>

πŸ› οΈ Development Workflow

  1. Create your HTML/JS/CSS files in your project
  2. Set up the server using one of the examples above
  3. Set the server file as HTTP trigger in Val Town
  4. Test your site - all files will be served automatically
  5. Add more files - just reference them in HTML, no server changes needed

πŸ” Debugging

Check Server Logs

Use the Val Town requests panel to see:

  • Which files are being requested
  • Response status codes
  • Any error messages
  • Middleware logs (if using advanced server)

Common Issues

404 Errors: Make sure file paths in HTML match actual file locations

<!-- If your file is at /assets/script.js --> <script src="/assets/script.js"></script>

MIME Type Issues: Add custom MIME types if needed

mimeTypes: { '.ts': 'text/typescript; charset=utf-8' }

SPA Routing: Use the SPA example for client-side routing

πŸš€ Deployment

  1. Your server file should be set as an HTTP trigger
  2. All other files (HTML, JS, CSS) are served automatically
  3. No build step required - files are served as-is
  4. Changes to files are reflected immediately

πŸ’‘ Tips

  • File Organization: Use subdirectories for better organization
  • Performance: Add appropriate cache headers for production
  • Security: Only serve files you intend to be public
  • Debugging: Use middleware for logging and monitoring
  • APIs: Mix static files with dynamic API endpoints easily

πŸ“š Examples in This Project

Check out the /examples/ directory for working examples:

  • basic-server.ts - Minimal setup
  • custom-server.ts - Custom configuration
  • advanced-server.ts - API routes + middleware
  • spa-server.ts - Single Page Application

Each example is a complete, working server you can copy and modify!

FeaturesVersion controlCode intelligenceCLI
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.