lws-blacklist Middleware

3.0.0 · maintenance · verified Wed Apr 22

lws-blacklist is a middleware package for the `lws` (local-web-server) ecosystem, designed to forbid specific routes based on regular expressions. It integrates with `lws` to add a `--blacklist` CLI option or can be configured programmatically. The package is currently at version `3.0.0`, published approximately six years ago, and is part of the `lwsjs` suite of modular `lws` plugins. While `lws` itself uses Koa as its middleware engine, `lws-blacklist` provides a simplified interface for defining forbidden paths, making it a specialized solution for access control within an `lws` server setup. Its primary differentiator is its tight integration and configuration synergy with `lws`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic setup of an lws server with lws-blacklist, blocking specific URL patterns like '/admin/(.*)' and '/secret-page.html' using the `--blacklist` option equivalent.

import Lws from 'lws';
import blacklistMiddleware from 'lws-blacklist';

const server = new Lws();

async function startServer() {
  try {
    const options = {
      port: 8000,
      stack: ['lws-static'], // Ensure static serving is also enabled
      directory: './public', // Serve files from a 'public' directory
      blacklist: ['/admin/(.*)', '/secret-page.html'], // Routes to forbid
      // For programmatic use, the middleware itself is added to the stack
      // but the configuration comes via options that lws-blacklist processes.
      // If lws-blacklist exported a direct Koa middleware, it would look like:
      // middleware: [blacklistMiddleware({ blacklist: ['/admin/(.*)'] })]
    };

    await server.start(options);
    console.log(`lws-blacklist example server running at http://localhost:${options.port}`);
    console.log('Try accessing http://localhost:8000/secret-page.html or http://localhost:8000/admin/dashboard.html');
    console.log('Serving static files from ./public');
  } catch (error) {
    console.error('Failed to start lws server:', error);
  }
}

// Create a public directory and some test files
import fs from 'fs';
if (!fs.existsSync('./public')) fs.mkdirSync('./public');
fs.writeFileSync('./public/index.html', '<h1>Hello from lws!</h1><p>Public page.</p>');
fs.writeFileSync('./public/secret-page.html', '<h1>ACCESS DENIED</h1><p>This page should be blocked.</p>');
fs.writeFileSync('./public/admin/dashboard.html', '<h1>Admin Dashboard</h1><p>This page should be blocked.</p>');

startServer();

view raw JSON →