Registry / http-networking / datahub-proxy-middleware

datahub-proxy-middleware

JSON →
library5.0.3jsnpmunverified

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-middleware
INSTALL
IMPORT
SIG · DATAHUB-PROXY-MIDD
D
datahub-proxy-middleware
http-networkingjavascriptv5.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

datahubMiddleware
const datahubMiddleware = require('datahub-proxy-middleware');
import datahubMiddleware from 'datahub-proxy-middleware';
This package primarily uses CommonJS `require`. There is no official ESM export.

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.

const express = require('express'); const datahubMiddleware = require('datahub-proxy-middleware'); const DataHub = require('macaca-datahub'); const app = express(); const port = 3000; // Minimal DataHub configuration for quickstart const datahubPort = 5678; const datahubConfig = { port: datahubPort, hostname: '127.0.0.1', proxy: { '/api': { hub: 'my_project_hub', port: datahubPort, hostname: 'localhost', pathOptions: { start: true } }, }, // In a real scenario, you'd have actual data in a store store: require('path').join(__dirname, 'data'), showBoard: false // Set to true to see the DataHub UI }; // Initialize and start DataHub server const datahubInstance = new DataHub({ port: datahubConfig.port, }); datahubInstance.startServer(datahubConfig).then(() => { console.log(`DataHub server ready on ${datahubConfig.hostname}:${datahubConfig.port}`); }); // Apply datahub-proxy-middleware to your Express app app.use(datahubMiddleware(app)(datahubConfig)); // Example route (will be proxied by datahubMiddleware if matching /api) app.get('/api/users', (req, res) => { res.json({ message: 'This should be mocked by DataHub!' }); }); // Fallback for non-proxied routes app.get('/', (req, res) => { res.send('Hello from Express!'); }); app.listen(port, () => { console.log(`Express app listening at http://localhost:${port}`); console.log('Try accessing http://localhost:3000/api/users (requires DataHub to serve mock data)'); });
Debug
Known issues
breakingThe underlying `path-to-regexp` library, which `datahub-proxy-middleware` utilizes for route matching, introduced breaking changes in its version 6. Users configuring advanced `pathOptions` should consult the `pillarjs/path-to-regexp` v6 documentation to ensure their configurations are compatible and to avoid unexpected routing behavior.
fix
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.
affects: >=5.0.0
gotchaThis middleware is designed for CommonJS environments using `require()`. Attempting to use ES module `import` syntax directly for `datahub-proxy-middleware` can lead to runtime errors, as it does not explicitly provide an ESM export.
fix
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.
affects: >=1.0.0
gotchaThe middleware expects an Express-like `app` object. If `app` is not correctly initialized or is not a compatible server instance (e.g., missing `app.use` method), the middleware will fail to attach.
fix
Ensure that the `app` object passed to `datahubMiddleware(app)` is a valid Express or Connect application instance, typically created via `const app = express();`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: app.use is not a function
`app` object passed to the middleware is not a valid Express or Connect application instance.
fix
Ensure `app` is initialized with `const app = express();` or similar before passing it to `datahubMiddleware(app)`.
Proxy error: connect ECONNREFUSED 127.0.0.1:5678
The `macaca-datahub` server is not running or is not accessible at the specified `port` and `hostname` in the `datahubConfig`.
fix
Verify that your `macaca-datahub` instance is running and listening on the correct `port` and `hostname` defined in `datahubConfig.proxy`.
No mock data found for path: /api/users
DataHub is running, but it does not have mock data configured for the requested path or hub.
fix
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.
Upgrade
Version history
5.0.3latest on npm
Audit
Dependencies
macaca-datahubrequiredThis package is a proxy for the macaca-datahub server; it's essential for providing the mock data backend.
expressoptionalThe middleware is designed to integrate with Express.js or Connect-style `app` instances. While not a direct dependency, it's implicitly required for typical usage.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
datahub-proxy-middleware — npm install datahub-proxy-middleware · libregistry