file-send
A http/https file send
Installation
$ npm install file-send
API
const url = ;const http = ;const through2 = ;const FileSend = ; http;
FileSend(request, path, [options])
Create a new FileSend
for the given path and options to initialize.
The request
is the Node.js HTTP request and the path
is a urlencoded path to send (urlencoded, not the actual file-system path).
Options
String
root - Set server root.
String|Array
ignore - Set ignore rules, support glob string. see: micromatch
String
ignoreAccess - Set how "ignore" are treated when encountered.
The default value is 'deny'
.
'deny'
Send a 403 for any request for ignore matched.'ignore'
Pretend like the ignore matched does not exist and 404.
Object
glob - Set micromatch options. see: micromatch
Boolean
acceptRanges - Enable or disable accepting ranged requests, defaults to true. Disabling this will not send Accept-Ranges and ignore the contents of the Range request header.
String
charset - Set Content-Type charset.
Boolean
cacheControl - Enable or disable setting Cache-Control
response header, defaults to true. Disabling this will ignore the immutable
and maxAge
options.
Boolean
etag - Enable or disable etag generation, defaults to true.
String|Array|Boolean
index - By default send supports "index.html" files, to disable this set false
or to supply a new index pass a string or an array in preferred order.
Boolean
lastModified - Enable or disable Last-Modified
header, defaults to true. Uses the file system's last modified value.
String|Number
maxAge - Provide a max-age in milliseconds for http caching, defaults to 0. This can also be a string accepted by the ms module.
Boolean
immutable - Enable or diable the immutable directive in the Cache-Control response header, defaults to false. If set to true, the maxAge option should also be specified to enable caching. The immutable directive will prevent supported clients from making conditional requests during the life of the maxAge option to check if the file has changed.
FileSend(request, path, [options]).pipe(response)
The pipe
method is used to pipe the response into the Node.js HTTP response object, typically FileSend(request, path, [options]).pipe(response)
.
FileSend.mime
The mime export is the global instance of of the mime-types
npm module.
Events
The FileSend
is an event emitter and will emit the following events:
dir
a directory was requested(realpath, next)
file
a file was requested(realpath, stats)
error
an error occurred(error, next)
Error-handling
By default when no 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.
Caching
It does 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's small enough that it does not need caching at all ;).
Running tests
$ npm install
$ npm test
Examples
'use strict'; const url = ;const http = ;const chalk = ;const cluster = ;const FileSend = ;const NUMCPUS = length; // create server { http ;} if clusterisMaster // fork workers for let i = 0; i < NUMCPUS; i++ const worker = cluster; // worker is listening if i === NUMCPUS - 1 worker; else // workers can share any tcp connection // in this case it is an http server ;