1.0.55 • Published 1 year ago

caching-proxy-middleware v1.0.55

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Caching-proxy-middleware

caching-proxy-middleware is a library that provides a middleware for caching HTTP proxy responses. It uses http-proxy-middleware to create a proxy and caches responses in the local file system. This library is particularly useful for reducing the load on the proxied server by serving cached responses when available.

Features

  • Automatic Caching: Caches the responses of proxied requests to the local file system.
  • Self-handled Responses: Intercepts and handles responses within the middleware.
  • JSON Support: Parses and stores JSON responses.
  • Directory Management: Automatically creates directories for storing cached files.

Installation

To install the library, use npm or yarn:

npm install caching-proxy-middleware --save-dev

OR

yarn add caching-proxy-middleware -D

Note: Upon installation, a new folder dev_cache will be generated and the .gitignore file will be updated automatically to include this new folder.

Basic Usage

server.middlewares.use(
  "/api",
  createCacheProxyMiddleware({
    target: "https://test-domain.example.com",
    changeOrigin: true,
    enableCaching: true, // NOTE: only enable this in development mode
  })
);

Sample Usage in frontend apps

Vite

Add a new plugin in vite.config.ts

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { createCacheProxyMiddleware } from "caching-proxy-middleware";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    {
      name: "Dev Caching", // Name it anything you want
      configureServer: (server) => {
        server.middlewares.use(
          "/cat-fact",
          createCacheProxyMiddleware({
            target: "https://cat-fact.herokuapp.com",
            changeOrigin: true,
            enableCaching: true, // only turn it on in development mode
            secure: false; // boolean | undefined. Verify SSL certificate.
          })
        );
      },
    },
  ],
});

Create React App

Create a file called setupProxy.js in the src directory of your CRA project. This file is automatically recognized by CRA.

const { createProxyMiddleware } = require("caching-proxy-middleware");

module.exports = function (app) {
  app.use(
    "/api",
    createCacheProxyMiddleware({
      target: "http://localhost:5000",
      enableCaching: true, // only turn it on in development mode
    })
  );
};

Roadmap

  • Selective Caching
  • Response Delay
  • Support more Content-Type (PDF, images etc)
  • Request Metadata Capture
1.0.55

1 year ago

1.0.54

1 year ago

1.0.53

1 year ago

1.0.52

1 year ago

1.0.46

1 year ago

1.0.43

1 year ago