\href{https://npmjs.org/package/send}{\texttt{ }} \href{https://npmjs.org/package/send}{\texttt{ }} \href{https://travis-ci.org/pillarjs/send}{\texttt{ }} \href{https://ci.appveyor.com/project/dougwilson/send}{\texttt{ }} \href{https://coveralls.io/r/pillarjs/send?branch=master}{\texttt{ }}

Send is a library for streaming files from the file system as a http response supporting partial responses (Ranges), conditional-\/\+GET negotiation (If-\/\+Match, If-\/\+Unmodified-\/\+Since, If-\/\+None-\/\+Match, If-\/\+Modified-\/\+Since), high test coverage, and granular events which may be leveraged to take appropriate actions in your application or framework.

Looking to serve up entire folders mapped to URLs? Try \href{https://www.npmjs.org/package/serve-static}{\texttt{ serve-\/static}}.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26687}{}\doxysection{Installation}\label{md_sfcode_node_modules_send_README_autotoc_md26687}
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 send }

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

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

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26689}{}\doxysubsection{send(req, path, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_send_README_autotoc_md26689}
Create a new {\ttfamily Send\+Stream} for the given path to send to a {\ttfamily res}. The {\ttfamily req} is the Node.\+js HTTP request and the {\ttfamily path} is a urlencoded path to send (urlencoded, not the actual file-\/system path).\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26690}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_send_README_autotoc_md26690}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26691}{}\doxyparagraph{accept\+Ranges}\label{md_sfcode_node_modules_send_README_autotoc_md26691}
Enable or disable accepting ranged requests, defaults to true. Disabling this will not send {\ttfamily Accept-\/\+Ranges} and ignore the contents of the {\ttfamily Range} request header.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26692}{}\doxyparagraph{cache\+Control}\label{md_sfcode_node_modules_send_README_autotoc_md26692}
Enable or disable setting {\ttfamily Cache-\/\+Control} response header, defaults to true. Disabling this will ignore the {\ttfamily immutable} and {\ttfamily max\+Age} options.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26693}{}\doxyparagraph{dotfiles}\label{md_sfcode_node_modules_send_README_autotoc_md26693}
Set how \char`\"{}dotfiles\char`\"{} are treated when encountered. A dotfile is a file or directory that begins with a dot (\char`\"{}.\char`\"{}). Note this check is done on the path itself without checking if the path actually exists on the disk. If {\ttfamily root} is specified, only the dotfiles above the root are checked (i.\+e. the root itself can be within a dotfile when when set to \char`\"{}deny\char`\"{}).


\begin{DoxyItemize}
\item `\textquotesingle{}allow'{\ttfamily No special treatment for dotfiles. -\/}\textquotesingle{}deny\textquotesingle{}{\ttfamily Send a 403 for any request for a dotfile. -\/}\textquotesingle{}ignore\textquotesingle{}\`{} Pretend like the dotfile does not exist and 404.
\end{DoxyItemize}

The default value is {\itshape similar} to `\textquotesingle{}ignore'\`{}, with the exception that this default will not ignore the files within a directory that begins with a dot, for backward-\/compatibility.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26694}{}\doxyparagraph{end}\label{md_sfcode_node_modules_send_README_autotoc_md26694}
Byte offset at which the stream ends, defaults to the length of the file minus 1. The end is inclusive in the stream, meaning {\ttfamily end\+: 3} will include the 4th byte in the stream.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26695}{}\doxyparagraph{etag}\label{md_sfcode_node_modules_send_README_autotoc_md26695}
Enable or disable etag generation, defaults to true.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26696}{}\doxyparagraph{extensions}\label{md_sfcode_node_modules_send_README_autotoc_md26696}
If a given file doesn\textquotesingle{}t exist, try appending one of the given extensions, in the given order. By default, this is disabled (set to {\ttfamily false}). An example value that will serve extension-\/less HTML files\+: `\mbox{[}\textquotesingle{}html', \textquotesingle{}htm\textquotesingle{}\mbox{]}\`{}. This is skipped if the requested file already has an extension.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26697}{}\doxyparagraph{immutable}\label{md_sfcode_node_modules_send_README_autotoc_md26697}
Enable or diable the {\ttfamily immutable} directive in the {\ttfamily Cache-\/\+Control} response header, defaults to {\ttfamily false}. If set to {\ttfamily true}, the {\ttfamily max\+Age} option should also be specified to enable caching. The {\ttfamily immutable} directive will prevent supported clients from making conditional requests during the life of the {\ttfamily max\+Age} option to check if the file has changed.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26698}{}\doxyparagraph{index}\label{md_sfcode_node_modules_send_README_autotoc_md26698}
By default send supports \char`\"{}index.\+html\char`\"{} files, to disable this set {\ttfamily false} or to supply a new index pass a string or an array in preferred order.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26699}{}\doxyparagraph{last\+Modified}\label{md_sfcode_node_modules_send_README_autotoc_md26699}
Enable or disable {\ttfamily Last-\/\+Modified} header, defaults to true. Uses the file system\textquotesingle{}s last modified value.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26700}{}\doxyparagraph{max\+Age}\label{md_sfcode_node_modules_send_README_autotoc_md26700}
Provide a max-\/age in milliseconds for http caching, defaults to 0. This can also be a string accepted by the \href{https://www.npmjs.org/package/ms\#readme}{\texttt{ ms}} module.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26701}{}\doxyparagraph{root}\label{md_sfcode_node_modules_send_README_autotoc_md26701}
Serve files relative to {\ttfamily path}.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26702}{}\doxyparagraph{start}\label{md_sfcode_node_modules_send_README_autotoc_md26702}
Byte offset at which the stream starts, defaults to 0. The start is inclusive, meaning {\ttfamily start\+: 2} will include the 3rd byte in the stream.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26703}{}\doxysubsubsection{Events}\label{md_sfcode_node_modules_send_README_autotoc_md26703}
The {\ttfamily Send\+Stream} is an event emitter and will emit the following events\+:


\begin{DoxyItemize}
\item {\ttfamily error} an error occurred {\ttfamily (err)}
\item {\ttfamily directory} a directory was requested {\ttfamily (res, path)}
\item {\ttfamily file} a file was requested {\ttfamily (path, stat)}
\item {\ttfamily headers} the headers are about to be set on a file {\ttfamily (res, path, stat)}
\item {\ttfamily stream} file streaming has started {\ttfamily (stream)}
\item {\ttfamily end} streaming has completed
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26704}{}\doxysubsubsection{.\+pipe}\label{md_sfcode_node_modules_send_README_autotoc_md26704}
The {\ttfamily pipe} method is used to pipe the response into the Node.\+js HTTP response object, typically {\ttfamily send(req, path, options).pipe(res)}.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26705}{}\doxysubsection{.\+mime}\label{md_sfcode_node_modules_send_README_autotoc_md26705}
The {\ttfamily mime} export is the global instance of of the \href{https://www.npmjs.com/package/mime}{\texttt{ {\ttfamily mime} npm module}}.

This is used to configure the MIME types that are associated with file extensions as well as other options for how to resolve the MIME type of a file (like the default type to use for an unknown file extension).\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26706}{}\doxysection{Error-\/handling}\label{md_sfcode_node_modules_send_README_autotoc_md26706}
By default when no {\ttfamily error} listeners are present an automatic response will be made, otherwise you have full control over the response, aka you may show a 5xx page etc.\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26707}{}\doxysection{Caching}\label{md_sfcode_node_modules_send_README_autotoc_md26707}
It does {\itshape not} perform internal caching, you should use a reverse proxy cache such as Varnish for this, or those fancy things called CDNs. If your application is small enough that it would benefit from single-\/node memory caching, it\textquotesingle{}s small enough that it does not need caching at all ;).\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26708}{}\doxysection{Debugging}\label{md_sfcode_node_modules_send_README_autotoc_md26708}
To enable {\ttfamily debug()} instrumentation output export {\bfseries{DEBUG}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ DEBUG=send node app }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26709}{}\doxysection{Running tests}\label{md_sfcode_node_modules_send_README_autotoc_md26709}

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

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26710}{}\doxysection{Examples}\label{md_sfcode_node_modules_send_README_autotoc_md26710}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26711}{}\doxysubsection{Serve a specific file}\label{md_sfcode_node_modules_send_README_autotoc_md26711}
This simple example will send a specific file to all requests.


\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var send = require('send')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  send(req, '/path/to/index.html')}
\DoxyCodeLine{    .pipe(res)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26712}{}\doxysubsection{Serve all files from a directory}\label{md_sfcode_node_modules_send_README_autotoc_md26712}
This simple example will just serve up all the files in a given directory as the top-\/level. For example, a request {\ttfamily GET /foo.txt} will send back {\ttfamily /www/public/foo.txt}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var parseUrl = require('parseurl')}
\DoxyCodeLine{var send = require('send')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  send(req, parseUrl(req).pathname, \{ root: '/www/public' \})}
\DoxyCodeLine{    .pipe(res)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26713}{}\doxysubsection{Custom file types}\label{md_sfcode_node_modules_send_README_autotoc_md26713}

\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var parseUrl = require('parseurl')}
\DoxyCodeLine{var send = require('send')}
\DoxyCodeLine{}
\DoxyCodeLine{// Default unknown types to text/plain}
\DoxyCodeLine{send.mime.default\_type = 'text/plain'}
\DoxyCodeLine{}
\DoxyCodeLine{// Add a custom type}
\DoxyCodeLine{send.mime.define(\{}
\DoxyCodeLine{  'application/x-\/my-\/type': ['x-\/mt', 'x-\/mtt']}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  send(req, parseUrl(req).pathname, \{ root: '/www/public' \})}
\DoxyCodeLine{    .pipe(res)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26714}{}\doxysubsection{Custom directory index view}\label{md_sfcode_node_modules_send_README_autotoc_md26714}
This is a example of serving up a structure of directories with a custom function to render a listing of a directory.


\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var fs = require('fs')}
\DoxyCodeLine{var parseUrl = require('parseurl')}
\DoxyCodeLine{var send = require('send')}
\DoxyCodeLine{}
\DoxyCodeLine{// Transfer arbitrary files from within /www/example.com/public/*}
\DoxyCodeLine{// with a custom handler for directory listing}
\DoxyCodeLine{var server = http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  send(req, parseUrl(req).pathname, \{ index: false, root: '/www/public' \})}
\DoxyCodeLine{    .once('directory', directory)}
\DoxyCodeLine{    .pipe(res)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000)}
\DoxyCodeLine{}
\DoxyCodeLine{// Custom directory handler}
\DoxyCodeLine{function directory (res, path) \{}
\DoxyCodeLine{  var stream = this}
\DoxyCodeLine{}
\DoxyCodeLine{  // redirect to trailing slash for consistent url}
\DoxyCodeLine{  if (!stream.hasTrailingSlash()) \{}
\DoxyCodeLine{    return stream.redirect(path)}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // get directory list}
\DoxyCodeLine{  fs.readdir(path, function onReaddir (err, list) \{}
\DoxyCodeLine{    if (err) return stream.error(err)}
\DoxyCodeLine{}
\DoxyCodeLine{    // render an index for the directory}
\DoxyCodeLine{    res.setHeader('Content-\/Type', 'text/plain; charset=UTF-\/8')}
\DoxyCodeLine{    res.end(list.join('\(\backslash\)n') + '\(\backslash\)n')}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_send_README_autotoc_md26715}{}\doxysubsection{Serving from a root directory with custom error-\/handling}\label{md_sfcode_node_modules_send_README_autotoc_md26715}

\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var parseUrl = require('parseurl')}
\DoxyCodeLine{var send = require('send')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function onRequest (req, res) \{}
\DoxyCodeLine{  // your custom error-\/handling logic:}
\DoxyCodeLine{  function error (err) \{}
\DoxyCodeLine{    res.statusCode = err.status || 500}
\DoxyCodeLine{    res.end(err.message)}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // your custom headers}
\DoxyCodeLine{  function headers (res, path, stat) \{}
\DoxyCodeLine{    // serve all files for download}
\DoxyCodeLine{    res.setHeader('Content-\/Disposition', 'attachment')}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // your custom directory handling logic:}
\DoxyCodeLine{  function redirect () \{}
\DoxyCodeLine{    res.statusCode = 301}
\DoxyCodeLine{    res.setHeader('Location', req.url + '/')}
\DoxyCodeLine{    res.end('Redirecting to ' + req.url + '/')}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // transfer arbitrary files from within}
\DoxyCodeLine{  // /www/example.com/public/*}
\DoxyCodeLine{  send(req, parseUrl(req).pathname, \{ root: '/www/public' \})}
\DoxyCodeLine{    .on('error', error)}
\DoxyCodeLine{    .on('directory', redirect)}
\DoxyCodeLine{    .on('headers', headers)}
\DoxyCodeLine{    .pipe(res)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

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