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
fs.readFile using the directory index as a fallback
var readfileDirectoryIndexFallback = require('readfile-directory-index-fallback');
// When the file `index.html` exists in the `foo` directory
readfileDirectoryIndexFallback('foo', function(err, buf) {
err; //=> null
buf.toString(); //=> the contents of `foo/index.html`
});
Installation
npm install readfile-directory-index-fallback
API
var 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,
- If the filePath points to an existing file, it passes the contents of the file to the callback.
- If nothing exists in filePath, it passes an error to the callback.
- If filePath points to an existing directory, it tries to read
index.html
(or the file specified indirectoryIndex
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'}, function(err, buf) {
err; //=> null
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', function(err, buf) {
err.code; //=> `EISDIR`
buf.toString(); //=> `undefined`
});
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 Shinnosuke Watanabe
Licensed under the MIT License.