Local Web Server

5.4.0 · active · verified Sun Apr 19

local-web-server is a lean, modular web server designed for rapid full-stack development. As a distribution of the core `lws` package, it bundles a starter pack of useful middleware, enabling functionalities like serving static files, Single Page Applications (SPAs), URL rewriting, CORS, and mock APIs out-of-the-box. The current stable version is 5.4.0, with regular updates addressing bug fixes and new features. It supports HTTP, HTTPS, and HTTP2, offering both programmatic and command-line interfaces. Key differentiators include its small footprint, modular design allowing users to load only required behaviors, and comprehensive features for prototyping back-end services or front-end applications. It requires Node.js v12.20 or newer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates programmatically configuring a local web server for SPA routing, API rewriting, CORS, and logging.

import createServer from 'local-web-server';

// Create a server instance with common development features
const server = createServer({
  port: process.env.PORT ? parseInt(process.env.PORT) : 8000,
  // Serve static files from the current directory
  // local-web-server includes static serving by default if 'root' is not specified.
  // This explicitly sets the root to the 'public' directory if it exists.
  root: './public',
  // Enable Single Page Application (SPA) routing, redirecting all non-file requests to index.html
  spa: 'index.html',
  // Enable CORS for all origins, useful for local development with separate API servers
  cors: true,
  // Rewrite API requests to a remote backend
  rewrite: {
    '/api/(.*)': 'https://jsonplaceholder.typicode.com/$1'
  },
  // Display a QR code in the terminal for easy mobile access
  qr: true,
  // Log requests in 'dev' format
  log: 'dev'
});

server.start();
console.log(`Local Web Server running at http://localhost:${server.port}`);
console.log(`Serving static files from: ${server.options.root || './'}`);

view raw JSON →