Node.js Static Server with Basic Auth

1.0.6 · abandoned · verified Wed Apr 22

node-static-auth provides a static file server for Node.js, integrating features like Basic authentication, access file logging with rotation, and custom error pages (401, 404, 500). It supports both HTTP and HTTPS, including an HTTP listener for automatic redirects to HTTPS, and historically offered experimental HTTP/2 support for Node.js versions 9.x and above. The package bundles popular modules like `node-static`, `basic-auth`, `morgan`, and `rotating-file-stream` to deliver its functionality. As of its last known stable release (1.0.6), it caters to older Node.js environments (engines >= 0.10.0) and has not seen active development in many years, making it unsuitable for modern applications requiring current security standards or active maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes and starts an HTTPS static server with Basic authentication and access logging, demonstrating common configuration options.

const NodeStaticAuth = require('node-static-auth');

// IMPORTANT: Replace 'path-to-public-directory' with the actual path to your static files.
// For example, if your static files are in a 'public' folder in the project root:
// const PUBLIC_DIR = path.join(process.cwd(), 'public');
// Ensure 'public' directory exists and contains 'index.html', 'your-forbidden.html', etc.

const config = {
    nodeStatic: {
        root: 'path-to-public-directory', // e.g., 'public'
        options: {
            indexFile: 'your-index.html' // e.g., 'index.html'
        },
        customPages: {
            forbidden: 'your-forbidden.html', // e.g., '401.html'
            notFound: 'your-not-found.html',   // e.g., '404.html'
            error: 'your-error.html'         // e.g., '500.html'
        }
    },
    server: {
        port: 3001,
        http2: false, // Set to true if running Node >= 9.x, but note limitations
        ssl: {
            enabled: true,
            httpListener: 3000,
            // Example: Replace with actual paths to your SSL certificate and key
            // key: path.join(process.cwd(), 'ssl', 'server.key'),
            // cert: path.join(process.cwd(), 'ssl', 'server.crt'),
            key: process.env.SSL_KEY_PATH ?? 'path/to/server.key', // Placeholder
            cert: process.env.SSL_CERT_PATH ?? 'path/to/server.crt' // Placeholder
        }
    },
    auth: {
        enabled: true,
        users: {
            admin: 'password123'
        }
    },
    logger: {
        enabled: true,
        path: 'access.log',
        type: 'combined', // Standard Apache combined log format
        rotation: {
            interval: '1d', // Rotate daily
            maxFiles: 10    // Keep a maximum of 10 rotated log files
        }
    }
};

const server = new NodeStaticAuth(config);
console.log(`Server running on HTTPS at https://localhost:${config.server.port} and HTTP listener on http://localhost:${config.server.ssl.httpListener}`);

view raw JSON →