Skip to content

Latest commit

 

History

History
58 lines (46 loc) · 1.98 KB

File metadata and controls

58 lines (46 loc) · 1.98 KB

API GET cache

Node http transparent proxy with in memory caching for GET calls.

This is mostly for debugging: I use it to cache an unstable system's script responses, so I can keep on working even if the remote server is being restarted (which may last for minutes).

Sometimes I need to work with http only api endpoints, but my local server needs to be https, so to avoid downgrading I can add my own cert. In an ideal world the backend api would return https with the same dev cert the frontend team uses.

Anything production related you are probably looking for something like Varnish.

env vars (nodemon.json)

{
  "env": {
    "USERNAME": "foo",
    "PASSWORD": "bar",
    "AUTH_BEARER": "eyJhbGcOIUz1NiJ9.foobar,bazqux",
    "TARGET": "https://api.github.com/",
    "MAX_WAIT_TIME": 1000,
    "TTL": 5000,
    "PORT": 4000,
    "MODIFY_HOST_HEADER": false,
    "HTTPS_PORT": 4002,
    "HTTPS_KEY": "/foo/bar/ca.key",
    "HTTPS_CERT": "/foo/bar/ca.crt",
    "CONTENT_BLACKLIST": "<h1>We'll be back shortly</h1>|<p>Temporarily unavailable due to maintenance</p>|>Initialization failed</h1>"
  }
}
  • USERNAME and PASSWORD are optional (if present, basic auth will be used)
  • AUTH_BEARER (inject as authorization bearer token into header), is optional
  • SSL section (cert, key, https_port) is optional
  • TTL is the cache item ttl in msec, use -1 for infinity, default is 5 sec
  • MODIFY_HOST_HEADER true replaces current host with target host in the request
  • CONTENT_BLACKLIST is a pipe separated list of strings (blacklisted resources will not be saved)

Defaults are in config.js.

usage

  1. npm i
  2. create nodemon.json
  3. npm run dev
  4. add yourhost.dev as 127.0.0.1 to your hosts file (if you want it to match with your cert)
  5. example call url: https://yourhost.dev:4000/users/octocat

internal api

All endpoints are GET, now we have a help page at /__help example