@ceicc/range v3.0.2
Range
Middleware for serving static files
Installation
npm i @ceicc/rangeUsage
add range to an existence express app
import { range } from "@ceicc/range"
app.get('/public/*', range())
app.listen(3000)This will serve every request starts with /public/ with range.
The base directory will be . or the current working directory, unless specified in the options object.
Options Object
maxAge
- default:
10800 type:
numbercaching period in seconds.
etag
- default:
true type:
booleanadd Etag header.
lastModified
- default:
true type:
booleanadd last-modified header.
conditional
- default:
true type:
booleanwhether to respect conditional requests or not.
range
- default:
true type:
booleanaccept range request.
notFound
- default:
true type:
boolean|stringa handler for non existing files
notFound: falsenextwill be called.notFound: trueempty body with status code '404' will be sent.notFound: <string>send a file with status code '404', the given string is the path to file.if the path doesn't led to a file,
nextwill be called.Note: The path is relative to the
baseDirpath.
implicitIndex
- default:
true type:
boolean|Array<string>Check for index files if the request path is a directory.
Pass an array of extensions to check against. e.g.
["html", "css"]Or simply pass
trueto check for html extension only.
baseDir
- default:
'.' type:
stringthe base dirctory.
hushErrors
- default:
false type:
booleanWhether to ignore errors and reply with status code
500, or pass the error tonextfunction.
trailingSlash
- default:
true type:
booleanRedirect directory requests to add trailing slash.
disabling this option will led to relative path issues. see #9
implicitIndexmust betrue
compression
- default:
false type:
false|Array<string>Compress the response body with one of the compression algorithm given in the array.
availabel compression methods are:
"br""gzip""deflate"the compression method will be determined based on the request's
accept-encodingheader using npm packagenegotiator.
dateHeader
- default:
true type:
booleansend
dateresponse header,new Date().toUTCString()function will be used to get the current date.learn more about the
dateheader
Real World Example
import { fileURLToPath } from "node:url"
import { join, dirname } from "node:path"
import express from "express"
import { range } from "@ceicc/range"
const app = express()
const __dirname = dirname(fileURLToPath(import.meta.url))
app.get('*', range({ baseDir: join(__dirname, "public") }))
app.use((error, req, res, next) => {
console.error(error)
res.sendStatus(500)
})
app.listen(3000, () => console.log("server listening on http://localhost:3000"))3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago