# @ceicc/range

> http range request handler

Latest version **3.0.2** (published 2022-12-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ceicc/range
pnpm add @ceicc/range
yarn add @ceicc/range
bun add @ceicc/range
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2022-12-08 |
| First published | 2021-09-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=14.16 |
| Dependencies | 3 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Husam Ahmed |
| Maintainers | ceicc |
| Keywords | range, range-header, partition |

## Links

- npm: https://www.npmjs.com/package/@ceicc/range
- Repository: https://github.com/ceicc/range
- Homepage: https://github.com/ceicc/range#readme
- Issues: https://github.com/ceicc/range/issues
- npm.io page: https://npm.io/package/@ceicc/range

## Dependencies (3)

- [mime-types](https://npm.io/package/mime-types.md) ^2.1.35
- [negotiator](https://npm.io/package/negotiator.md) ^0.6.3
- [compressible](https://npm.io/package/compressible.md) ^2.0.18

## Recent versions

- 3.0.2 (latest) — 2022-12-08
- 3.0.0-no-types-dir (test) — 2022-08-23
- 3.0.0-beta.8 (next) — 2022-06-13
- 3.0.1 — 2022-08-23
- 3.0.0 — 2022-06-15
- 2.2.2 — 2022-06-05
- 3.0.0-beta.7 — 2022-06-05
- 3.0.0-beta.6 — 2022-06-05
- 3.0.0-beta.5 — 2022-05-28
- 3.0.0-beta.4 — 2022-05-04
- 3.0.0-beta.3 — 2022-04-17
- 3.0.0-beta.2 — 2022-03-20
- 3.0.0-beta.1 — 2022-02-17
- 2.2.1 — 2022-02-02
- 2.2.0 — 2022-02-01
- … 5 more at https://npm.io/package/@ceicc/range/versions

## README

# Range
Middleware for serving static files

[![NPM Downloads per month](https://img.shields.io/npm/dm/@ceicc/range?logo=npm&style=flat-square&color=088)](https://npmjs.com/package/@ceicc/range)
[![Module type](https://img.shields.io/badge/Module-ESM-informational?style=flat-square)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)

## Installation
```bash
npm i @ceicc/range
```

## Usage

add `range` to an existence express app

```js
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: `number`

  caching period in seconds.

#### `etag`

  - default: `true`
  - type: `boolean`

  add Etag header.

#### `lastModified`

  - default: `true`
  - type: `boolean`

  add last-modified header.

#### `conditional`

  - default: `true`
  - type: `boolean`

  whether to respect conditional requests or not.

#### `range`

  - default: `true`
  - type: `boolean`

  accept range request.

#### `notFound`

  - default: `true`
  - type: `boolean|string`

  a handler for non existing files

  `notFound: false` `next` will be called.

  `notFound: true` empty 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, `next` will be called.

  ***Note:*** The path is relative to the `baseDir` path.

#### `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 `true` to check for html extension only.

#### `baseDir`

  - default: `'.'`
  - type: `string`

  the base dirctory.

#### `hushErrors`

  - default: `false`
  - type: `boolean`

  Whether to ignore errors and reply with status code `500`, or pass the error to `next` function.

#### `trailingSlash`

  - default: `true`
  - type: `boolean`

  Redirect directory requests to add trailing slash.

  disabling this option will led to relative path issues. [see #9](https://github.com/Ceicc/range/issues/9)

  `implicitIndex` must be `true`

#### `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:
  1. `"br"`
  1. `"gzip"`
  1. `"deflate"`

  the compression method will be determined based on the request's [`accept-encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/accept-encoding) header using npm package [`negotiator`](https://npmjs.com/package/negotiator).

#### `dateHeader`

  - default: `true`
  - type: `boolean`

  send `date` response header, `new Date().toUTCString()` function will be used to get the current date.

  [learn more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date) about the `date` header


## Real World Example

```js
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"))
```

---
_Source: https://npm.io/package/@ceicc/range · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
