cache url normaliser
Express middleware to populate a
res.cache
object with consistent URL parsing for use in cache keys.
Motivation
This simple middleware module provides an extra layer of URL parsing to provide an object useful for setting/getting cache keys within the request/response cycle. It normalises query string objects and provides suitable base and path strings. The resulting object is useful for finer granular cache keys in the context of an API server.
Output res.cache
For a/b/c/endpoint?c=12&order=asc&y=some_value&a=1&z=67
:
Query Strings
For example all of the following:
endpoint?a=1&z=67&c=12&y=some_value&order=asc
endpoint?a=1&z=67&c=12&order=asc&y=some_value
endpoint?c=12&order=asc&y=some_value&a=1&z=67
...resolve to:
endpoint?a=1&c=12&order=asc&y=some_value&z=67
Example Use (with Redis HSET/HGET/HDEL
)
For example in the sample res.cache
above, we could use the concatenation of pathname
(/a/b/c
) and basename
(endpoint
) as the key
, base
(endpoint?a=1&c=12&order=asc&y=some_value&z=67
) as the field
and the data response from whatever data source as the value
....
TO DO
Caveats
Nested properties (using [
, ]
), e.g. /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
as described here won't be parsed correctly.
License
MIT