http-mockserver is a Node.js library designed for easily mocking HTTP servers, primarily used for testing purposes. It allows developers to define static or dynamic responses for specific HTTP methods and URIs on a given port. The current stable version is 2.7.21 (as of April 2026). The package appears to have a consistent maintenance cadence, with recent updates (as seen in the changelog for 2025-2026) focusing on dependency vulnerability fixes and minor bug fixes rather than new features or breaking changes. Key differentiators include its simplicity in defining mock endpoints, support for both static responses and dynamic request handlers (using Express-like `req`, `res` objects), and the ability to start a mock server either programmatically within a Node.js process or as a standalone CLI tool. The CLI option allows interaction from non-Node.js clients via a REST API. It also provides utilities for inspecting request logs and waiting for specific requests to simplify assertion logic in tests.
npm install http-mockserverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up both static and dynamic HTTP mocks on different ports using `http-mockserver`. It shows how to define responses with specific status codes, headers, and bodies, and how to use a handler function for more complex, stateful mocking. It also illustrates creating port-specific instances.
Ensure your Node.js environment meets the minimum version requirement (>=v16.20.2). Use `nvm` or `volta` to manage Node.js versions if necessary.
Choose an unused port for your mocks, or implement logic to dynamically find an available port. Always clean up mocks with `mockServer.clearAll()` or `mockServer.stop()` in your test teardown to free up ports.
If managing the mock server within the same process, use `mockServer`. If communicating with a remote or independently launched mock server, configure `mockClient.setServerHost()` and use its API methods.
Change the `port` number in your `addMock` call to an available port. Alternatively, ensure `mockServer.clearAll()` and `mockServer.stop()` are called in your test suite's `afterAll` or `teardown` hooks to prevent port conflicts.
Ensure you are using the correct named import: `import { mockServer } from 'http-mockserver';` for ESM or `const { mockServer } = require('http-mockserver');` for CommonJS. `mockServer` is an object, not a class.Explicitly define the `port` property within each mock object passed to `mockServer.addMock({ port: 8080, ... })`. If you want to avoid specifying the port repeatedly, use `const myMockService = mockServer.create(8080);` and then `myMockService.addMock({ ... });`.No dependency data recorded yet.