datahub-proxy-middleware is an Express/Connect-compatible HTTP server middleware designed to proxy API requests to a Macaca.js DataHub instance. It facilitates API mocking and data management during development by allowing developers to define flexible proxy rules that redirect specific paths to a running DataHub server for mock data retrieval. The current stable version, 5.0.3, was released in March 2024, indicating active maintenance within the Macaca.js ecosystem. Key differentiators include its tight integration with `macaca-datahub`, robust configuration options for defining proxy routes (e.g., dynamic ports, hostnames, and path matching with `@macaca-project/path-to-regexp`), and its suitability for development environments, particularly when integrated with tools like `webpack-dev-server` to provide mock data without requiring a live backend.
npm install datahub-proxy-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up an Express server with `datahub-proxy-middleware` to proxy API requests to a `macaca-datahub` instance. It initializes both DataHub and the Express application, showing how to integrate the middleware for local development with mocked APIs.
Review the official documentation for `pillarjs/path-to-regexp` version 6 for changes in syntax, options, and behavior related to path matching. Adjust `pathOptions` in your `datahubConfig` accordingly.
Always use `const datahubMiddleware = require('datahub-proxy-middleware');` when importing the package. If using in an ES module context, consider a transpilation step or a dynamic `import()` if absolutely necessary.Ensure that the `app` object passed to `datahubMiddleware(app)` is a valid Express or Connect application instance, typically created via `const app = express();`.
Ensure `app` is initialized with `const app = express();` or similar before passing it to `datahubMiddleware(app)`.
Verify that your `macaca-datahub` instance is running and listening on the correct `port` and `hostname` defined in `datahubConfig.proxy`.
Ensure that your `macaca-datahub` instance has the correct 'hub' activated and that mock data for the requested API paths (e.g., `/api/users`) is defined in your DataHub store.