\href{https://npmjs.org/package/compression}{\texttt{ }} \href{https://npmjs.org/package/compression}{\texttt{ }} \href{https://travis-ci.org/expressjs/compression}{\texttt{ }} \href{https://coveralls.io/r/expressjs/compression?branch=master}{\texttt{ }}

Node.\+js compression middleware.

The following compression codings are supported\+:


\begin{DoxyItemize}
\item deflate
\item gzip
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6110}{}\doxysection{Install}\label{md_sfcode_node_modules_compression_README_autotoc_md6110}
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 compression }

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

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

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6112}{}\doxysubsection{compression(\mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_compression_README_autotoc_md6112}
Returns the compression middleware using the given {\ttfamily options}. The middleware will attempt to compress response bodies for all request that traverse through the middleware, based on the given {\ttfamily options}.

This middleware will never compress responses that include a {\ttfamily Cache-\/\+Control} header with the \href{https://tools.ietf.org/html/rfc7234\#section-5.2.2.4}{\texttt{ {\ttfamily no-\/transform} directive}}, as compressing will transform the body.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6113}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_compression_README_autotoc_md6113}
{\ttfamily compression()} accepts these properties in the options object. In addition to those listed below, \href{http://nodejs.org/api/zlib.html}{\texttt{ zlib}} options may be passed in to the options object.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6114}{}\doxyparagraph{chunk\+Size}\label{md_sfcode_node_modules_compression_README_autotoc_md6114}
The default value is {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+CHUNK}, or {\ttfamily 16384}.

See \href{http://nodejs.org/api/zlib.html\#zlib_memory_usage_tuning}{\texttt{ Node.\+js documentation}} regarding the usage.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6115}{}\doxyparagraph{filter}\label{md_sfcode_node_modules_compression_README_autotoc_md6115}
A function to decide if the response should be considered for compression. This function is called as {\ttfamily filter(req, res)} and is expected to return {\ttfamily true} to consider the response for compression, or {\ttfamily false} to not compress the response.

The default filter function uses the \href{https://www.npmjs.com/package/compressible}{\texttt{ compressible}} module to determine if `res.\+get\+Header(\textquotesingle{}Content-\/\+Type')\`{} is compressible.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6116}{}\doxyparagraph{level}\label{md_sfcode_node_modules_compression_README_autotoc_md6116}
The level of zlib compression to apply to responses. A higher level will result in better compression, but will take longer to complete. A lower level will result in less compression, but will be much faster.

This is an integer in the range of {\ttfamily 0} (no compression) to {\ttfamily 9} (maximum compression). The special value {\ttfamily -\/1} can be used to mean the \char`\"{}default compression level\char`\"{}, which is a default compromise between speed and compression (currently equivalent to level 6).


\begin{DoxyItemize}
\item {\ttfamily -\/1} Default compression level (also {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+COMPRESSION}).
\item {\ttfamily 0} No compression (also {\ttfamily zlib.\+Z\+\_\+\+NO\+\_\+\+COMPRESSION}).
\item {\ttfamily 1} Fastest compression (also {\ttfamily zlib.\+Z\+\_\+\+BEST\+\_\+\+SPEED}).
\item {\ttfamily 2}
\item {\ttfamily 3}
\item {\ttfamily 4}
\item {\ttfamily 5}
\item {\ttfamily 6} (currently what {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+COMPRESSION} points to).
\item {\ttfamily 7}
\item {\ttfamily 8}
\item {\ttfamily 9} Best compression (also {\ttfamily zlib.\+Z\+\_\+\+BEST\+\_\+\+COMPRESSION}).
\end{DoxyItemize}

The default value is {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+COMPRESSION}, or {\ttfamily -\/1}.

{\bfseries{Note}} in the list above, {\ttfamily zlib} is from `zlib = require(\textquotesingle{}zlib')\`{}.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6117}{}\doxyparagraph{mem\+Level}\label{md_sfcode_node_modules_compression_README_autotoc_md6117}
This specifies how much memory should be allocated for the internal compression state and is an integer in the range of {\ttfamily 1} (minimum level) and {\ttfamily 9} (maximum level).

The default value is {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+MEMLEVEL}, or {\ttfamily 8}.

See \href{http://nodejs.org/api/zlib.html\#zlib_memory_usage_tuning}{\texttt{ Node.\+js documentation}} regarding the usage.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6118}{}\doxyparagraph{strategy}\label{md_sfcode_node_modules_compression_README_autotoc_md6118}
This is used to tune the compression algorithm. This value only affects the compression ratio, not the correctness of the compressed output, even if it is not set appropriately.


\begin{DoxyItemize}
\item {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+STRATEGY} Use for normal data.
\item {\ttfamily zlib.\+Z\+\_\+\+FILTERED} Use for data produced by a filter (or predictor). Filtered data consists mostly of small values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress them better. The effect is to force more Huffman coding and less string matching; it is somewhat intermediate between {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+STRATEGY} and {\ttfamily zlib.\+Z\+\_\+\+HUFFMAN\+\_\+\+ONLY}.
\item {\ttfamily zlib.\+Z\+\_\+\+FIXED} Use to prevent the use of dynamic Huffman codes, allowing for a simpler decoder for special applications.
\item {\ttfamily zlib.\+Z\+\_\+\+HUFFMAN\+\_\+\+ONLY} Use to force Huffman encoding only (no string match).
\item {\ttfamily zlib.\+Z\+\_\+\+RLE} Use to limit match distances to one (run-\/length encoding). This is designed to be almost as fast as {\ttfamily zlib.\+Z\+\_\+\+HUFFMAN\+\_\+\+ONLY}, but give better compression for PNG image data.
\end{DoxyItemize}

{\bfseries{Note}} in the list above, {\ttfamily zlib} is from `zlib = require(\textquotesingle{}zlib')\`{}.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6119}{}\doxyparagraph{threshold}\label{md_sfcode_node_modules_compression_README_autotoc_md6119}
The byte threshold for the response body size before compression is considered for the response, defaults to {\ttfamily 1kb}. This is a number of bytes or any string accepted by the \href{https://www.npmjs.com/package/bytes}{\texttt{ bytes}} module.

{\bfseries{Note}} this is only an advisory setting; if the response size cannot be determined at the time the response headers are written, then it is assumed the response is {\itshape over} the threshold. To guarantee the response size can be determined, be sure set a {\ttfamily Content-\/\+Length} response header.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6120}{}\doxyparagraph{window\+Bits}\label{md_sfcode_node_modules_compression_README_autotoc_md6120}
The default value is {\ttfamily zlib.\+Z\+\_\+\+DEFAULT\+\_\+\+WINDOWBITS}, or {\ttfamily 15}.

See \href{http://nodejs.org/api/zlib.html\#zlib_memory_usage_tuning}{\texttt{ Node.\+js documentation}} regarding the usage.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6121}{}\doxysubsubsection{.\+filter}\label{md_sfcode_node_modules_compression_README_autotoc_md6121}
The default {\ttfamily filter} function. This is used to construct a custom filter function that is an extension of the default function.


\begin{DoxyCode}{0}
\DoxyCodeLine{var compression = require('compression')}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{app.use(compression(\{ filter: shouldCompress \}))}
\DoxyCodeLine{}
\DoxyCodeLine{function shouldCompress (req, res) \{}
\DoxyCodeLine{  if (req.headers['x-\/no-\/compression']) \{}
\DoxyCodeLine{    // don't compress responses with this request header}
\DoxyCodeLine{    return false}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // fallback to standard filter function}
\DoxyCodeLine{  return compression.filter(req, res)}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6122}{}\doxysubsection{res.\+flush}\label{md_sfcode_node_modules_compression_README_autotoc_md6122}
This module adds a {\ttfamily res.\+flush()} method to force the partially-\/compressed response to be flushed to the client.\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6123}{}\doxysection{Examples}\label{md_sfcode_node_modules_compression_README_autotoc_md6123}
\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6124}{}\doxysubsection{express/connect}\label{md_sfcode_node_modules_compression_README_autotoc_md6124}
When using this module with express or connect, simply {\ttfamily app.\+use} the module as high as you like. Requests that pass through the middleware will be compressed.


\begin{DoxyCode}{0}
\DoxyCodeLine{var compression = require('compression')}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{}
\DoxyCodeLine{// compress all responses}
\DoxyCodeLine{app.use(compression())}
\DoxyCodeLine{}
\DoxyCodeLine{// add all routes }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_compression_README_autotoc_md6125}{}\doxysubsection{Server-\/\+Sent Events}\label{md_sfcode_node_modules_compression_README_autotoc_md6125}
Because of the nature of compression this module does not work out of the box with server-\/sent events. To compress content, a window of the output needs to be buffered up in order to get good compression. Typically when using server-\/sent events, there are certain block of data that need to reach the client.

You can achieve this by calling {\ttfamily res.\+flush()} when you need the data written to actually make it to the client.


\begin{DoxyCode}{0}
\DoxyCodeLine{var compression = require('compression')}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{}
\DoxyCodeLine{// compress responses}
\DoxyCodeLine{app.use(compression())}
\DoxyCodeLine{}
\DoxyCodeLine{// server-\/sent event stream}
\DoxyCodeLine{app.get('/events', function (req, res) \{}
\DoxyCodeLine{  res.setHeader('Content-\/Type', 'text/event-\/stream')}
\DoxyCodeLine{  res.setHeader('Cache-\/Control', 'no-\/cache')}
\DoxyCodeLine{}
\DoxyCodeLine{  // send a ping approx every 2 seconds}
\DoxyCodeLine{  var timer = setInterval(function () \{}
\DoxyCodeLine{    res.write('data: ping\(\backslash\)n\(\backslash\)n')}
\DoxyCodeLine{}
\DoxyCodeLine{    // !!! this is the important part}
\DoxyCodeLine{    res.flush()}
\DoxyCodeLine{  \}, 2000)}
\DoxyCodeLine{}
\DoxyCodeLine{  res.on('close', function () \{}
\DoxyCodeLine{    clearInterval(timer)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\}) }

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