JSPM

readfile-directory-index-fallback

2.0.0-0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q44864F
  • License MIT

fs.readFile using the directory index as a fallback

Package Exports

  • readfile-directory-index-fallback

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (readfile-directory-index-fallback) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

readfile-directory-index-fallback

NPM version Build Status Build status Coverage Status Dependency Status devDependency Status

fs.readFile using the directory index as a fallback

const readfileDirectoryIndexFallback = require('readfile-directory-index-fallback');

// When the file `index.html` exists in the `foo` directory
readfileDirectoryIndexFallback('foo', (err, buf) => {
  buf.toString(); //=> the contents of `foo/index.html`
});

Installation

Use npm.

npm install readfile-directory-index-fallback

API

const readfileDescendantFallback = require('readfile-directory-index-fallback');

readfileDirectoryIndexFallback(filePath,[ options,] callback)

filePath: String
options: Object (fs.readFile options) or String (encoding)
callback: Function

First, it tries to read a file at filePath. Then,

  1. If the filePath points to an existing file, it passes the contents of the file to the callback.
  2. If nothing exists in filePath, it passes an error to the callback.
  3. If filePath points to an existing directory, it tries to read index.html (or the file specified in directoryIndex option) immediately under filePath directory.

options

In addition to the following, all fs.readFile options are available.

options.directoryIndex

Type: String or Boolean
Default: index.html

A filename of the directory index contents (e.g. index.php).

// When the file `home.html` exists in the `site/contents` directory
readfileDirectoryIndexFallback('site/contents', {directoryIndex: 'home.html'}, (err, buf) => {
  buf.toString(); //=> the contents of `site/contents/index.html`
});

false disables the fallback feature, that is, this function becomes the same as fs.readFile.

// Even if index.html exists in the `foo` directory
readfileDirectoryIndexFallback('foo', {directoryIndex: false}, err => {
  err.code; //=> `EISDIR`
});

callback(error, buffer)

error: Error if it fails to read a file, otherwise null
buffer: Buffer or String (according to fs.readFile option)

It automatically strips UTF-8 byte order mark from the result.

License

Copyright (c) 2014 - 2015 Shinnosuke Watanabe

Licensed under the MIT License.