Skip to content

rhagni/file-get-contents.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fileGetContents.js

A Node.js version of file_get_contents PHP function

Objective

Turn easy handling a request to a URL or to File System wrapping into a Promise

Example

const fileGetContents = require('file-get-contents');

// A File request

try {
	let data = await fileGetContents('/tmp/foo/bar');

	console.log(data);
} catch (err) {
	console.log('Unable to load data from /tmp/foo/bar');
}

// Or a HTTP(S) request

fileGetContents('https://pokeapi.co/api/v2/pokemon/1/').then(json => {
	const pokemon = JSON.parse(json);

	console.log(`Name of first pokemon is ${pokemon.name}`);
}).catch(err => {
	console.err(`Unable to get content from PokeAPI. Reason: ${err.message}`);
});

About

A Node.js version of file_get_contents PHP function

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published