light-my-request is a utility library designed to simulate HTTP requests against Node.js HTTP servers without requiring the server to be bound to a network port or even in a listening state. This makes it an ideal tool for writing fast, isolated tests for server logic, as well as for debugging server-side applications. The current stable version is 6.6.0. The project maintains a consistent release cadence, frequently delivering minor versions and patch updates to address features, bug fixes, and performance improvements, as evidenced by the regular 6.x releases. A core differentiator of this library is its ability to interact directly with an `http.createServer` dispatch function, effectively injecting fake request and response objects without relying on actual socket connections. It provides flexibility by supporting both traditional callback-based request handling and modern Promise-based (async/await) patterns, alongside a fluent, chainable API for constructing complex requests.
npm install light-my-requestVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic GET and POST request injection using the async/await and chained API, showing how to interact with a standard Node.js HTTP dispatch function.
Ensure your server logic under test is designed to be independent of network specifics or provide mocks for network-dependent features if necessary.
Use `import { inject } from 'light-my-request'` instead of `const inject = require('light-my-request')` in ESM files. For TypeScript, ensure your `tsconfig.json` correctly configures `moduleResolution`.Migrate from `inject(dispatch, options, (err, res) => { ... })` to `inject(dispatch, options).then(res => { ... }).catch(err => { ... })` or `try { const res = await inject(dispatch, options); } catch (err) { ... }`.In an ESM file (`.mjs` or `type: 'module'` in `package.json`), use `import { inject } from 'light-my-request';`. If using TypeScript, ensure correct `moduleResolution` in `tsconfig.json`.Ensure you `await` the `inject` call when using `async/await`, or handle the `.then()` and `.catch()` branches correctly when using Promises. For example: `const res = await inject(...); console.log(res.payload);`
Install `form-auto-content` if you intend to use it for `multipart/form-data` requests: `npm install form-auto-content`.