Search code examples
javascriptnode.jsexpressnpmcache-control

Parsing cache-control header value from response


I need to parse the cache-control header response to use it from caching the API response.

Sample response headers:

{"x-backside-transport":"OK OK","connection":"close","transfer-encoding":"chunked","access-control-allow-origin":"*","cache-control":"public, max-age=60","content-type":"application/json","date":"Thu, 05 Oct 2017 10:33:59 GMT","etag":"\"-161458570\"","x-powered-by":"Express","x-global-transaction-id":"429271565"}

I am using console.log("------ CACHE VALUE----"+JSON.stringify(response.headers.cache-control)); but this seems to fail everytime.

Giving me the error: ReferenceError: control is not defined

But when it try the same with

console.log("------ CACHE VALUE----"+JSON.stringify(response.headers.connection));

I am able to print out log value successfully. Am i missing something?


Solution

  • Dash - is not applicable in JSON. You may want to try parse like below

    response.headers["cache-control"]