Node.js program local file operation usage summary
- 2021-01-22 04:52:55
- OfStack
Node prides itself on having a very small core. Some languages have full POSIX and API bindings, while Node implements as few bindings as possible and exposes them in synchronous, asynchronous, or streaming API form.
This approach means that there are some very convenient features in the operating system that need to be rebuilt in Node. This is a practical tutorial that teaches you how to use file system packages.
Reference file
One of the most important things about interacting with the file system is pointing to the correct file. Because NPM packages use relative path references, you can't write paths in code. There are two main ways to ensure that packages refer to the correct files:
// use `path.join()` Rather than `+` Make sure that Windows And it works
const path = require('path')
// Find the relative path based on the call point for the command line program (CLI applications) Very practical
path.join(process.cwd(), 'my-dynamic-file')
// or
path.resolve('my-dynamic-file')
// Based on the 1 A file is found additionally 1 A file
path.join(__dirname, 'my-package-file')
Read the file
The easiest way to asynchronously read a file in a node is to use a stream! Here's an example:
const path = require('path')
const fs = require('fs')
// read a file and pipe it to the console
fs.createReadStream(path.join(__dirname, 'my-file'))
.pipe(process.stdout)
Create a file
It's not that hard to create a file. Here's an cat command implemented with node:
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(__dirname, './my-other-file')))
Delete the file
The rm-rf command is usually used for files and directories to be deleted in Shell scripts. One 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 directory
Creating a file is similar to deleting it, using the ES39en 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 files in all subdirectories
// to the command line. The object stream must be
// stringified before being passed to `stdout`.
readdirp({ root: path.join(__dirname) })
.pipe(json.stringify())
.pipe(process.stdout)
Find a file in the current parent directory using findup:
const findup = require('findup')
const path = require('path')
// recurse up 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 Piping (pipes)
It is very important to process the entire data stream error once in the pipeline. Instead of using.on('error', cb) for each individual data stream:
const pump = require('pump')
const fs = require('fs')
// oh no, no errors are handled!
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
})