CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
README.md258 linesDownload Raw Back to serve-static
1# serve-static2 3[![NPM Version][npm-version-image]][npm-url]4[![NPM Downloads][npm-downloads-image]][npm-url]5[![Linux Build][github-actions-ci-image]][github-actions-ci-url]6[![Windows Build][appveyor-image]][appveyor-url]7[![Test Coverage][coveralls-image]][coveralls-url]8 9## Install10 11This is a [Node.js](https://nodejs.org/en/) module available through the12[npm registry](https://www.npmjs.com/). Installation is done using the13[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):14 15```sh16$ npm install serve-static17```18 19## API20 21```js22var serveStatic = require('serve-static')23```24 25### serveStatic(root, options)26 27Create a new middleware function to serve files from within a given root28directory. The file to serve will be determined by combining `req.url`29with the provided root directory. When a file is not found, instead of30sending a 404 response, this module will instead call `next()` to move on31to the next middleware, allowing for stacking and fall-backs.32 33#### Options34 35##### acceptRanges36 37Enable or disable accepting ranged requests, defaults to true.38Disabling this will not send `Accept-Ranges` and ignore the contents39of the `Range` request header.40 41##### cacheControl42 43Enable or disable setting `Cache-Control` response header, defaults to44true. Disabling this will ignore the `immutable` and `maxAge` options.45 46##### dotfiles47 48 Set how "dotfiles" are treated when encountered. A dotfile is a file49or directory that begins with a dot ("."). Note this check is done on50the path itself without checking if the path actually exists on the51disk. If `root` is specified, only the dotfiles above the root are52checked (i.e. the root itself can be within a dotfile when set53to "deny").54 55  - `'allow'` No special treatment for dotfiles.56  - `'deny'` Deny a request for a dotfile and 403/`next()`.57  - `'ignore'` Pretend like the dotfile does not exist and 404/`next()`.58 59The default value is similar to `'ignore'`, with the exception that this60default will not ignore the files within a directory that begins with a dot.61 62##### etag63 64Enable or disable etag generation, defaults to true.65 66##### extensions67 68Set file extension fallbacks. When set, if a file is not found, the given69extensions will be added to the file name and search for. The first that70exists will be served. Example: `['html', 'htm']`.71 72The default value is `false`.73 74##### fallthrough75 76Set the middleware to have client errors fall-through as just unhandled77requests, otherwise forward a client error. The difference is that client78errors like a bad request or a request to a non-existent file will cause79this middleware to simply `next()` to your next middleware when this value80is `true`. When this value is `false`, these errors (even 404s), will invoke81`next(err)`.82 83Typically `true` is desired such that multiple physical directories can be84mapped to the same web address or for routes to fill in non-existent files.85 86The value `false` can be used if this middleware is mounted at a path that87is designed to be strictly a single file system directory, which allows for88short-circuiting 404s for less overhead. This middleware will also reply to89all methods.90 91The default value is `true`.92 93##### immutable94 95Enable or disable the `immutable` directive in the `Cache-Control` response96header, defaults to `false`. If set to `true`, the `maxAge` option should97also be specified to enable caching. The `immutable` directive will prevent98supported clients from making conditional requests during the life of the99`maxAge` option to check if the file has changed.100 101##### index102 103By default this module will send "index.html" files in response to a request104on a directory. To disable this set `false` or to supply a new index pass a105string or an array in preferred order.106 107##### lastModified108 109Enable or disable `Last-Modified` header, defaults to true. Uses the file110system's last modified value.111 112##### maxAge113 114Provide a max-age in milliseconds for http caching, defaults to 0. This115can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)116module.117 118##### redirect119 120Redirect to trailing "/" when the pathname is a dir. Defaults to `true`.121 122##### setHeaders123 124Function to set custom headers on response. Alterations to the headers need to125occur synchronously. The function is called as `fn(res, path, stat)`, where126the arguments are:127 128  - `res` the response object129  - `path` the file path that is being sent130  - `stat` the stat object of the file that is being sent131 132## Examples133 134### Serve files with vanilla node.js http server135 136```js137var finalhandler = require('finalhandler')138var http = require('http')139var serveStatic = require('serve-static')140 141// Serve up public/ftp folder142var serve = serveStatic('public/ftp', { index: ['index.html', 'index.htm'] })143 144// Create server145var server = http.createServer(function onRequest (req, res) {146  serve(req, res, finalhandler(req, res))147})148 149// Listen150server.listen(3000)151```152 153### Serve all files as downloads154 155```js156var contentDisposition = require('content-disposition')157var finalhandler = require('finalhandler')158var http = require('http')159var serveStatic = require('serve-static')160 161// Serve up public/ftp folder162var serve = serveStatic('public/ftp', {163  index: false,164  setHeaders: setHeaders165})166 167// Set header to force download168function setHeaders (res, path) {169  res.setHeader('Content-Disposition', contentDisposition(path))170}171 172// Create server173var server = http.createServer(function onRequest (req, res) {174  serve(req, res, finalhandler(req, res))175})176 177// Listen178server.listen(3000)179```180 181### Serving using express182 183#### Simple184 185This is a simple example of using Express.186 187```js188var express = require('express')189var serveStatic = require('serve-static')190 191var app = express()192 193app.use(serveStatic('public/ftp', { index: ['default.html', 'default.htm'] }))194app.listen(3000)195```196 197#### Multiple roots198 199This example shows a simple way to search through multiple directories.200Files are searched for in `public-optimized/` first, then `public/` second201as a fallback.202 203```js204var express = require('express')205var path = require('path')206var serveStatic = require('serve-static')207 208var app = express()209 210app.use(serveStatic(path.join(__dirname, 'public-optimized')))211app.use(serveStatic(path.join(__dirname, 'public')))212app.listen(3000)213```214 215#### Different settings for paths216 217This example shows how to set a different max age depending on the served218file type. In this example, HTML files are not cached, while everything else219is for 1 day.220 221```js222var express = require('express')223var path = require('path')224var serveStatic = require('serve-static')225 226var app = express()227 228app.use(serveStatic(path.join(__dirname, 'public'), {229  maxAge: '1d',230  setHeaders: setCustomCacheControl231}))232 233app.listen(3000)234 235function setCustomCacheControl (res, path) {236  if (serveStatic.mime.lookup(path) === 'text/html') {237    // Custom Cache-Control for HTML files238    res.setHeader('Cache-Control', 'public, max-age=0')239  }240}241```242 243## License244 245[MIT](LICENSE)246 247[appveyor-image]: https://badgen.net/appveyor/ci/dougwilson/serve-static/master?label=windows248[appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-static249[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/serve-static/master250[coveralls-url]: https://coveralls.io/r/expressjs/serve-static?branch=master251[github-actions-ci-image]: https://badgen.net/github/checks/expressjs/serve-static/master?label=linux252[github-actions-ci-url]: https://github.com/expressjs/serve-static/actions/workflows/ci.yml253[node-image]: https://badgen.net/npm/node/serve-static254[node-url]: https://nodejs.org/en/download/255[npm-downloads-image]: https://badgen.net/npm/dm/serve-static256[npm-url]: https://npmjs.org/package/serve-static257[npm-version-image]: https://badgen.net/npm/v/serve-static258