HTTP Server for Single Page Applications

1.3.0 · active · verified Sun Apr 19

http-server-spa is a lightweight and fast Node.js-based static file server specifically designed to host Single Page Applications (SPAs). Its primary feature is built-in support for the HTML5 History API fallback, which routes all non-file requests to a specified fallback HTML file (e.g., `index.html`), allowing client-side routers to handle URL paths without 404 errors. This differentiates it from general-purpose static servers that require additional configuration for this behavior. The package is predominantly a command-line interface (CLI) tool, commonly installed globally. While the npm metadata indicates version 1.3.0, the most recent "first official release" is tagged as 3.2.0, addressing several bug fixes including Node.js compatibility. Release cadence appears irregular, focusing on critical bug fixes and improvements rather than a fixed schedule. It distinguishes between "file requests" and "route requests," serving actual files with a 200 status or redirecting route requests to the fallback with a 301 status for non-root paths.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to globally install `http-server-spa` and use its CLI to serve a basic single-page application, showcasing the history-api-fallback functionality for client-side routing and serving static assets.

// To get started, first ensure Node.js is installed.

// 1. Install http-server-spa globally using npm:
//    npm install -g http-server-spa

// 2. Create a directory for your Single Page Application (SPA):
//    mkdir my-spa-app
//    cd my-spa-app

// 3. Create a basic 'index.html' file which will act as your SPA's entry point
//    and the fallback file:
//    echo '<!DOCTYPE html>\n<html>\n<head>\n  <title>My SPA</title>\n  <style>body { font-family: sans-serif; text-align: center; margin-top: 50px; }</style>\n</head>\n<body>\n  <h1>Welcome to my SPA!</h1>\n  <div id="app"></div>\n  <script>\n    document.getElementById("app").innerHTML = "Current path: <b>" + window.location.pathname + "</b>";\n    // Basic client-side router simulation for demonstration
    window.onpopstate = () => {
      console.log('Path changed to:', window.location.pathname);
      document.getElementById("app").innerHTML = "Current path: <b>" + window.location.pathname + "</b>";
    };
  </script>\n</body>\n</html>' > index.html

// 4. Optionally, create a static asset, e.g., a CSS file:
//    mkdir assets
//    echo 'body { background-color: #f0f0f0; }' > assets/style.css

// 5. Start the http-server-spa. This command serves the current directory ('.'),
//    uses 'index.html' as the fallback for all non-file routes, and listens on port 8080.
//    Open your terminal in the 'my-spa-app' directory and run:
//    http-server-spa . index.html 8080

// After running the command, open your web browser and navigate to:
// - http://localhost:8080/ (serves index.html, status 200)
// - http://localhost:8080/about (serves index.html, status 301, client-side router handles path)
// - http://localhost:8080/user/profile (serves index.html, status 301, client-side router handles path)
// - http://localhost:8080/assets/style.css (serves the actual CSS file, status 200)

// Press Ctrl+C in the terminal to stop the server.

view raw JSON →