The `basic` package provides a minimalist HTTP Basic Authentication middleware for Node.js applications. It abstracts the process of parsing the `Authorization` header and validating user credentials against a provided callback function. As of its latest stable version, 2.0.3 (published over seven years ago), the package is primarily designed for CommonJS environments and integrates directly with Node.js's native `http` module. It follows a callback-based asynchronous pattern for credential verification. Its primary differentiation lies in its extreme simplicity and lack of external dependencies, making it a lightweight option for adding basic authentication to a server. Due to its age, it receives infrequent updates and may not natively support modern JavaScript features like Promises or ES Modules without additional wrappers.
npm install basicVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a basic HTTP server with basic authentication using the `basic` package, handling success and 401 responses.
Use `const basic = require('basic');` for importing the module.Ensure `res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Secure Area"'});` is called when authentication fails, replacing 'Secure Area' with your desired realm.Consider wrapping the authentication function in a Promise for easier integration with async/await patterns, or use a more modern authentication middleware for new projects.
Change your import to `const basic = require('basic');` as the package is CommonJS-only.On authentication failure, ensure you set the `WWW-Authenticate` header: `res.writeHead(401, {'WWW-Authenticate': 'Basic realm="Secure Area"'});`Double-check your `user` and `pass` comparison logic and ensure `callback(null)` is called when credentials are valid, and `callback(401)` (or another error code) when invalid.
No dependency data recorded yet.