\href{https://npmjs.org/package/fresh}{\texttt{ }} \href{https://npmjs.org/package/fresh}{\texttt{ }} \href{https://nodejs.org/en/}{\texttt{ }} \href{https://travis-ci.org/jshttp/fresh}{\texttt{ }} \href{https://coveralls.io/r/jshttp/fresh?branch=master}{\texttt{ }}

HTTP response freshness testing\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10882}{}\doxysection{Installation}\label{md_sfcode_node_modules_fresh_README_autotoc_md10882}
This is a \href{https://nodejs.org/en/}{\texttt{ Node.\+js}} module available through the \href{https://www.npmjs.com/}{\texttt{ npm registry}}. Installation is done using the \href{https://docs.npmjs.com/getting-started/installing-npm-packages-locally}{\texttt{ {\ttfamily npm install} command}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install fresh }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10883}{}\doxysection{API}\label{md_sfcode_node_modules_fresh_README_autotoc_md10883}

\begin{DoxyCode}{0}
\DoxyCodeLine{var fresh = require('fresh') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10884}{}\doxysubsection{fresh(req\+Headers, res\+Headers)}\label{md_sfcode_node_modules_fresh_README_autotoc_md10884}
Check freshness of the response using request and response headers.

When the response is still \char`\"{}fresh\char`\"{} in the client\textquotesingle{}s cache {\ttfamily true} is returned, otherwise {\ttfamily false} is returned to indicate that the client cache is now stale and the full response should be sent.

When a client sends the {\ttfamily Cache-\/\+Control\+: no-\/cache} request header to indicate an end-\/to-\/end reload request, this module will return {\ttfamily false} to make handling these requests transparent.\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10885}{}\doxysection{Known Issues}\label{md_sfcode_node_modules_fresh_README_autotoc_md10885}
This module is designed to only follow the HTTP specifications, not to work-\/around all kinda of client bugs (especially since this module typically does not recieve enough information to understand what the client actually is).

There is a known issue that in certain versions of Safari, Safari will incorrectly make a request that allows this module to validate freshness of the resource even when Safari does not have a representation of the resource in the cache. The module \href{https://www.npmjs.com/package/jumanji}{\texttt{ jumanji}} can be used in an Express application to work-\/around this issue and also provides links to further reading on this Safari bug.\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10886}{}\doxysection{Example}\label{md_sfcode_node_modules_fresh_README_autotoc_md10886}
\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10887}{}\doxysubsection{API usage}\label{md_sfcode_node_modules_fresh_README_autotoc_md10887}

\begin{DoxyCode}{0}
\DoxyCodeLine{var reqHeaders = \{ 'if-\/none-\/match': '"{}foo"{}' \}}
\DoxyCodeLine{var resHeaders = \{ 'etag': '"{}bar"{}' \}}
\DoxyCodeLine{fresh(reqHeaders, resHeaders)}
\DoxyCodeLine{// => false}
\DoxyCodeLine{}
\DoxyCodeLine{var reqHeaders = \{ 'if-\/none-\/match': '"{}foo"{}' \}}
\DoxyCodeLine{var resHeaders = \{ 'etag': '"{}foo"{}' \}}
\DoxyCodeLine{fresh(reqHeaders, resHeaders)}
\DoxyCodeLine{// => true }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10888}{}\doxysubsection{Using with Node.\+js http server}\label{md_sfcode_node_modules_fresh_README_autotoc_md10888}

\begin{DoxyCode}{0}
\DoxyCodeLine{var fresh = require('fresh')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  // perform server logic}
\DoxyCodeLine{  // ... including adding ETag / Last-\/Modified response headers}
\DoxyCodeLine{}
\DoxyCodeLine{  if (isFresh(req, res)) \{}
\DoxyCodeLine{    // client has a fresh copy of resource}
\DoxyCodeLine{    res.statusCode = 304}
\DoxyCodeLine{    res.end()}
\DoxyCodeLine{    return}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // send the resource}
\DoxyCodeLine{  res.statusCode = 200}
\DoxyCodeLine{  res.end('hello, world!')}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{function isFresh (req, res) \{}
\DoxyCodeLine{  return fresh(req.headers, \{}
\DoxyCodeLine{    'etag': res.getHeader('ETag'),}
\DoxyCodeLine{    'last-\/modified': res.getHeader('Last-\/Modified')}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_fresh_README_autotoc_md10889}{}\doxysection{License}\label{md_sfcode_node_modules_fresh_README_autotoc_md10889}
\mbox{[}MIT\mbox{]}(LICENSE) 