Gathering detailed insights and metrics for express-serve-zip
Gathering detailed insights and metrics for express-serve-zip
Gathering detailed insights and metrics for express-serve-zip
Gathering detailed insights and metrics for express-serve-zip
npm install express-serve-zip
Typescript
Module System
Min. Node Version
Node Version
NPM Version
71.5
Supply Chain
98.5
Quality
73.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
49,561
Last Day
14
Last Week
1,061
Last Month
3,399
Last Year
16,698
MIT License
1 Stars
9 Commits
1 Forks
2 Watchers
1 Branches
1 Contributors
Updated on Dec 16, 2023
Latest Version
1.1.0
Package Id
express-serve-zip@1.1.0
Unpacked Size
35.78 kB
Size
10.81 kB
File Count
5
NPM Version
10.2.4
Node Version
20.11.1
Published on
Feb 21, 2024
Cumulative downloads
Total Downloads
Last Day
-95.2%
14
Compared to previous day
Last Week
71.1%
1,061
Compared to previous week
Last Month
37.4%
3,399
Compared to previous month
Last Year
43.1%
16,698
Compared to previous year
[![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url]
Based on serve-static v1.15.0 and send v0.18.0.
Using @yarnpkg/fslib for virtual filesystem support
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
1$ npm install express-serve-zip
1var serveZip = require('express-serve-zip')
Create a new middleware function to serve files from within a given root
directory. The file to serve will be determined by combining req.url
with the provided root directory. When a file is not found, instead of
sending a 404 response, this module will instead call next()
to move on
to the next middleware, allowing for stacking and fall-backs.
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.
Enable or disable setting Cache-Control
response header, defaults to
true. Disabling this will ignore the immutable
and maxAge
options.
Set how "dotfiles" are treated when encountered. A dotfile is a file
or directory that begins with a dot ("."). Note this check is done on
the path itself without checking if the path actually exists on the
disk. If root
is specified, only the dotfiles above the root are
checked (i.e. the root itself can be within a dotfile when set
to "deny").
'allow'
No special treatment for dotfiles.'deny'
Deny a request for a dotfile and 403/next()
.'ignore'
Pretend like the dotfile does not exist and 404/next()
.The default value is similar to 'ignore'
, with the exception that this
default will not ignore the files within a directory that begins with a dot.
Enable or disable etag generation, defaults to true.
Set file extension fallbacks. When set, if a file is not found, the given
extensions will be added to the file name and search for. The first that
exists will be served. Example: ['html', 'htm']
.
The default value is false
.
Set the middleware to have client errors fall-through as just unhandled
requests, otherwise forward a client error. The difference is that client
errors like a bad request or a request to a non-existent file will cause
this middleware to simply next()
to your next middleware when this value
is true
. When this value is false
, these errors (even 404s), will invoke
next(err)
.
Typically true
is desired such that multiple physical directories can be
mapped to the same web address or for routes to fill in non-existent files.
The value false
can be used if this middleware is mounted at a path that
is designed to be strictly a single file system directory, which allows for
short-circuiting 404s for less overhead. This middleware will also reply to
all methods.
The default value is true
.
Enable or disable 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.
By default this module will send "index.html" files in response to a request
on a directory. To disable this set false
or to supply a new index pass a
string or an array in preferred order.
Enable or disable Last-Modified
header, defaults to true. Uses the file
system's last modified value.
Provide a max-age in milliseconds for http caching, defaults to 0. This can also be a string accepted by the ms module.
Redirect to trailing "/" when the pathname is a dir. Defaults to true
.
Function to set custom headers on response. Alterations to the headers need to
occur synchronously. The function is called as fn(res, path, stat)
, where
the arguments are:
res
the response objectpath
the file path that is being sentstat
the stat object of the file that is being sent1var finalhandler = require('finalhandler') 2var http = require('http') 3var serveStatic = require('serve-static') 4 5// Serve up public/ftp folder 6var serve = serveStatic('public/ftp', { index: ['index.html', 'index.htm'] }) 7 8// Create server 9var server = http.createServer(function onRequest (req, res) { 10 serve(req, res, finalhandler(req, res)) 11}) 12 13// Listen 14server.listen(3000)
1var contentDisposition = require('content-disposition') 2var finalhandler = require('finalhandler') 3var http = require('http') 4var serveStatic = require('serve-static') 5 6// Serve up public/ftp folder 7var serve = serveStatic('public/ftp', { 8 index: false, 9 setHeaders: setHeaders 10}) 11 12// Set header to force download 13function setHeaders (res, path) { 14 res.setHeader('Content-Disposition', contentDisposition(path)) 15} 16 17// Create server 18var server = http.createServer(function onRequest (req, res) { 19 serve(req, res, finalhandler(req, res)) 20}) 21 22// Listen 23server.listen(3000)
This is a simple example of using Express.
1var express = require('express') 2var serveStatic = require('serve-static') 3 4var app = express() 5 6app.use(serveStatic('public/ftp', { index: ['default.html', 'default.htm'] })) 7app.listen(3000)
This example shows a simple way to search through multiple directories.
Files are searched for in public-optimized/
first, then public/
second
as a fallback.
1var express = require('express') 2var path = require('path') 3var serveStatic = require('serve-static') 4 5var app = express() 6 7app.use(serveStatic(path.join(__dirname, 'public-optimized'))) 8app.use(serveStatic(path.join(__dirname, 'public'))) 9app.listen(3000)
This example shows how to set a different max age depending on the served file type. In this example, HTML files are not cached, while everything else is for 1 day.
1var express = require('express') 2var path = require('path') 3var serveStatic = require('serve-static') 4 5var app = express() 6 7app.use(serveStatic(path.join(__dirname, 'public'), { 8 maxAge: '1d', 9 setHeaders: setCustomCacheControl 10})) 11 12app.listen(3000) 13 14function setCustomCacheControl (res, path) { 15 if (serveStatic.mime.lookup(path) === 'text/html') { 16 // Custom Cache-Control for HTML files 17 res.setHeader('Cache-Control', 'public, max-age=0') 18 } 19}
MIT [npm-url]: https://npmjs.org/package/express-serve-zip [npm-version-image]: https://badgen.net/npm/v/express-serve-zip
No vulnerabilities found.