GithubHelp home page GithubHelp logo

programingjd / bit.node.handlers.static Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 112 KB

nodejs http handler for serving static files from memory, with compression and etags.

License: MIT License

JavaScript 98.20% HTML 1.66% CSS 0.13% C 0.02%
nodejs http handler static

bit.node.handlers.static's Introduction

Node.js version Bit.dev package GitHub package.json version GitHub Travis (.org) Coveralls github

Node.js module.

Http handler for serving static files.

All files of supported types are processed and loaded in memory.

ETag are computed for relevant file types.

Compressed versions (gzip and brotli) are stored for relevant file types.

There's a special endpoint to trigger a synchronization event and re-read the files from disk.

Usage

const http = require('http');
const staticDirectory = require('@bit/programingjd.node.handlers.static');

(async()=>{
  const handler = await staticDirectory({root: 'www'});
  http.createServer((request, response)=>{
    const accepted = handler.accept(request, response, 'not_used', request.connection.remoteAddress);
    if (accepted) {
      handler.handle(accepted);
    } else {
      response.writeHead(404);
      response.end();
    }
  }).listen(80); 
})();

Options

  • root (string)

    The path of the directory to serve.

    It defaults to 'www'.

  • prefix (string)

    The path prefix to use for serving the files.

    It defaults to '' (no prefix).

    Example:

    root = 'www' and prefix = 'files'

    /files/doc.html on the server points to ./www/doc.html on disk.

  • disallowSharedCache (boolean)

    If you require authorization to access these static files, you can prevent browsers from storing the cached data in a shared cache by setting this option to true.

    It defaults to false (caching in a shared cache is allowed).

  • headers (object)

    You can override the default headers (hsts, csp, ...) by specifying your own values. By default, the security restrictions are secure and you will need the security headers if you want to load external resources for instance.

  • allowedFileTypes (object)

    You can override the default list of supported file types and provide your own.

    It defaults to the built-in file type list (see below for details).

    Example:

    {
      "html": {
        "headers": {
          "Content-Type": "text/html",
          "Cache-Control": "public,no-cache" 
        },
        "compress": true
      },
      "css": {
        "headers": {
          "Content-Type": "text/css",
          "Cache-Control": "public,no-cache" 
        },
        "compress": true
      },
      "js": {
        "headers": {
          "Content-Type": "application/javascript",
          "Cache-Control": "public,no-cache" 
        },
        "compress": true
      }
    }

    Note that for the headers, you need to provide at least Content-Type and Cache-Control. For Cache-Control, you have to provide the public directive even if it is the default, so that it can be replaced with private automatically when the disallowSharedCache is set.

    Setting compress to true triggers the automatic gzip and brotli compression.

Default file types

File extensions

  • .js (javascript file)
  • .mjs (javascript module)
  • .css (css file)
  • .map (css or javascript map file)
  • .htm and .html (html file)
  • .txt (text file)
  • .csv (csv file)
  • .md (markdown file)
  • .adoc (asciidoc file)
  • .xml (xml file)
  • .gpx (gpx file)
  • .json (json file)
  • .jsonld (json-ld file)
  • .geojson (geojson file)
  • .topojson (topojson file)
  • .yml and .yaml (yaml file)
  • .woff (woff font file)
  • .woff2 (woff2 font file)
  • .jpg (jpeg image)
  • .png (png image)
  • .svg (svg image)
  • .ico (icon image)
  • .webp (webp image)
  • .mp4 (mp4 video)
  • .webm (webm video)
  • .zip (zip archive)
  • .epub (epub ebook)
  • .pdf (pdf document)
  • .wav (wav audio)
  • .mp3 (mp3 audio)
  • .aac (aac audio)
  • .wasm (wasm binary file)
  • .wat (wasm text file)
  • .sig (pgp signature file)
  • .bin (binary file)
  • .glsl (glsl shader)
  • .gltf (gltf model)
  • .glb (gltf binary model)
  • .manifest (web manifest)

bit.node.handlers.static's People

Contributors

programingjd avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.