Connect-prism is a Node.js middleware designed to record, mock, and proxy HTTP traffic, primarily for front-end development and end-to-end testing of Single Page Applications (SPAs). It functions by integrating with the `connect` middleware framework, allowing developers to capture API responses, store them as JSON files, and then replay them locally, effectively caching backend interactions. The package supports four modes: 'record' (captures live responses), 'mock' (serves recorded responses), 'mock & record' (prioritizes mocks, records if not found), and 'proxy' (forwards requests to a target API). The current stable version, 1.0.21, was last published in early 2019, indicating an abandoned or minimally maintained status. Its key differentiators include its tight integration with `connect` servers for in-development mocking and its VCR-like approach to HTTP interaction capture, accelerating test suites and reducing reliance on live backend services during development. It decompresses gzipped/deflated responses for readable mock files.
npm install connect-prismVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up a basic `connect` server with `connect-prism` middleware. It demonstrates how to configure prism to proxy requests to an external API (JSONPlaceholder), record responses to a local 'mocks' directory, or serve previously recorded mocks. It uses environment variables to switch between 'record' and 'mock' modes.
Consider migrating to actively maintained HTTP mocking libraries such as 'nock' (for Node.js tests) or 'MSW' (Mock Service Worker for browser/Node.js) which offer broader compatibility and active development.
Verify directory permissions (`chmod 755 <mocks_path>`) and ensure the `mocksPath` configured exists and is writable by the user running the Node.js process.
For consistent mock usage, normalize requests where possible, or use the 'override' feature if manual control over mock responses for specific request variations is needed. Regularly review generated mock files to ensure desired behavior.
Ensure you have `const connect = require('connect');` and `const app = connect();` and that `app.use(prism.middleware);` is called after `prism.create({ ... });`.Check and adjust the file system permissions for your `mocksPath` directory. For example, `chmod -R 777 /path/to/mocks` (for development, be cautious in production) or ensure the user running the Node.js application owns the directory.
Double-check the `context` path to ensure it matches the API endpoint your application is calling. Verify `host`, `port`, and `https` settings accurately reflect the target API. Ensure `app.use(prism.middleware);` is positioned early enough in your `connect` middleware stack to intercept API requests.