Node's proudest boast is that it has a very small core. Some languages bind to the full POSIX API, and node implements as few bindings as possible and exposes them in the form of synchronous, asynchronous, or streaming APIs.
This approach means that there are some very handy features in the operating system that need to be rebuilt in node. This is a practical tutorial that teaches you how to use the file system software package.
referencing files
It is important to interact with the file system to point to the correct file. Because NPM's packages use relative path references, you can't write the path to death in code. There are two main ways to ensure that packages can be referenced to the correct file:
Use ' path.join () ' instead of ' + ' to make sure Windows works as well.
Const PATH = require (' path ')
//Find a relative path based on the call point, for a command-line program (CLI applications) Very practical
Path.join (PROCESS.CWD (), ' my-dynamic-file ')
//
path.resolve (' My-dynamic-file ')
//Find another file based on one file
path.join (__dirname, ' my-package-file ')
reading files
the easiest way to read files asynchronously in a node is to use the stream! Here is an example:
Const PATH = require (' path ')
const FS = require (' FS ')
//read a file and pipe it to the console
Fs.createre Adstream (Path.join (__dirname, ' my-file '))
. Pipe (Process.stdout)
Create a file
creating a file is not difficult, and here's a cat command with node implementation:
Const PATH = require (' path ')
const FS = require (' FS ')
//cat./my-file >/my-other-file
Fs.createreadstream (Path.join (__dirname, ' my-file ')). Pipe (Fs.createwritestream (Path.join
, './ My-other-file '))
deleting files
files and directories that are deleted in a shell script typically use the RM-RF command. A Rimraf in Nodejs also implements the same function:
Const RIMRAF = require (' Rimraf ')
const PATH = require (' path ')
Rimraf (Path.join (__dirname, './my-directory ') Err => {
if (err) throw err
})
Create a table of contents
creating is similar to deleting a file, using the MKDIRP package
Const MKDIRP = require (' MKDIRP ')
const PATH = require (' path ')
mkdirp (Path.join (__dirname, ' Foo/bar '), err = > {
if (err) throw err
})
Find Files
use READDIRP to find files in the current directory:
Const READDIRP = require (' READDIRP ')
const JSON = require (' Jsonstream ')
const PATH = require (' path ')
// Recursively print out all subdirectories/to the
command line. The object stream must is
//stringified before being passed to ' stdout '.
Readdirp ({root:path.join (__dirname)})
. Pipe (Json.stringify ())
. Pipe (Process.stdout)
Use Findup to find files in the current parent directory:
Const FINDUP = require (' findup ')
const PATH = require (' path ')
//recurse All files relative to __dirname and Find
//All ' Package.json ' files.
Findup (Path.join (__dirname), ' Package.json ', (err, res) => {
if (err) throw err
console.log (' dir is: ' + res)
})
About Pipelines (pipes)
It is very important to handle errors in the entire data stream once in the pipeline. Instead of using each individual data stream. On (' Error ', CB):
Const PUMP = require (' pump ')
const FS = require (' FS ')
//Oh no, no errors
are Fs.createreadstream ('./in.file '). Pipe (Fs.createwritestream ('./out.file '))
//That ' s better, we ' re handing Errors now
Const RS = Fs.createreadstream ('./in.file ')
const WS = Fs.createwritestream ('./out.file ')
pump (RS, WS, err => {
if (err) throw err
})