takeout
v0.3.0
Published
Get the file contents, seamlessly available for both local file system and HTTP(S)
Downloads
3
Maintainers
Readme
takeout
A Node module to get the file contents, seamlessly available for both local file system and HTTP(S)
var takeout = require('takeout');
// Reading a local file
takeout('path/to/local/file', {encoding: 'utf8'}, function(err, body) {
if (err) {
throw err;
}
console.log(body);
});
// GET request if the location is URL
takeout('http://nodejs.org', {encoding: 'utf8'}, function(err, body, res) {
if (err) {
throw err;
}
console.log(res.statusCode);
console.log(body);
});
Installation
npm install takeout
API
var takeout = require('takeout');
takeout(location,[ options,] callback)
location: String
(local file path or URL)
options: Object
callback: Function
If the location is a local file path, it reads the file with fs.readFile. If the locatiandon is a URL, it makes a GET request to the URL and grabs its response, using got.
// `http` and `https` scheme is optional.
takeout('www.npmjs.org', function(err, body, res) {
if (err) {
throw err;
}
console.log(res.statusCode);
console.log(body);
});
options
In addition to the following, all options for fs.readFile and got are available.
options.encoding
Type: String
or null
Default: null
(In other words, the content is returned as a Buffer
by default.)
Directly passed to fs.readFile options or setEncoding
of the response data.
options.directoryIndex
Type: Boolean
or String
of filename
Default: index.html
When the path indicates a local directory, it reads index.html
or a file with the specified filename immediately under the directory.
false
disables this feature.
// |
// +- dist
// +- index.html ('foo')
// +- home.html ('bar')
takeout('dist', function(err, buf) {
!err; //=> true
buf.toString(); //=> 'foo'
});
takeout('dist', {directoryIndex: 'home.html'}, function(err, buf) {
!err; //=> true
buf.toString(); //=> 'bar'
});
takeout('dist', {directoryIndex: false}, function(err) {
err.code; //=> 'ENOTFOUND'
});
callback(error, body, response)
error: Error
if it fails to get the contents, otherwise null
body: Buffer
or String
(according to options.encoding
)
response: Object
(response object) if the content is got via HTTP(S), otherwise undefined
CLI
You can use this module as a CLI tool by installing it globally.
npm install -g takeout
Usage
Usage1: takeout <file path | URL> --out <dest>
Usage2: takeout <file path | URL> > <dest>
Options:
--out, -o <file path> Write the result to a file (stdout by default)
--encoding, -e <encoding> Set encoding (same as Node's encoding option)
--index, -d <filename> Set the filename of directory index
--no-index, Do not care about directory index
--timeout, -t <ms> Set time after which the request will be aborted
--help, -h Print usage information
--version, -v Print version
You can do almost the same thing with cat
and curl
. But this tool works on various environments, as long as they supports Node.
License
Copyright (c) 2014 Shinnosuke Watanabe
Licensed under the MIT License.