@wooksjs/http-static
TypeScript icon, indicating that this package has built-in type declarations

0.4.35 • Public • Published

Wooks Serve File

!!! This is work-in-progress library, breaking changes are expected !!!


Wooks Serve File is composable static file server for @wooksjs/event-http.

serveFile returns a readable stream and prepares all the neccessary response headers (like content-length, content-type etc).

  • ✅ returns a readable stream
  • ✅ prepares all the neccessary response headers (like content-length, content-type etc)
  • ✅ can handle etag
  • ✅ can handle ranges

Installation

npm install @wooksjs/http-static

Usage

import { serveFile } from '@wooksjs/http-static'
// ...
serveFile(filePath, options)

serveFile options

{
    // Any header to add
    headers?: Record<string, string>,

    // Cache-Control header
    cacheControl?: TCacheControl,

    // Expires header
    expires?: Date | string | number,

    // when true a header "Pragma: no-cache" will be added
    pragmaNoCache?: boolean,

    // the base directory path
    baseDir?: string,

    // default extension will be added to the filePath
    defaultExt?: string,

    // when true lists files in directory
    listDirectory?: boolean,

    // put 'index.html'
    // to automatically serve it from the folder
    index?: string,
}

Built-in file server example:

import { useRouteParams } from 'wooks'
import { serveFile } from '@wooksjs/http-static'
app.get('static/*', () => {
  const { get } = useRouteParams()
  return serveFile(get('*'), { cacheControl: { maxAge: '10m' } })
})

cacheControl here is the same object as used in useSetCacheControl().setCacheControl({ ... }) from @wooksjs/composables

Documentation

To check out docs, visit wooks.moost.org.

Package Sidebar

Install

npm i @wooksjs/http-static

Weekly Downloads

5

Version

0.4.35

License

MIT

Unpacked Size

108 kB

Total Files

5

Last publish

Collaborators

  • mav-rik