Summary of local file operations in the Node. js program and node. js
Node has a very small core. Some languages bind complete POSIX APIs, while Node implements as few bindings as possible and exposes them through synchronous, asynchronous, or stream APIs.
This method means that the operating system has some very convenient functions and needs to be rebuilt in the Node. This is a practical tutorial that teaches you how to use the file system software package.
Reference File
An important aspect of interaction with a file system is to point to the correct file. Because the NPM package uses relative path reference, you cannot write the path to the code. There are two main methods to ensure that the package can reference the correct file:
// Use 'path. join () 'instead of' + 'ensures that Windows can work normally. const path = require ('path') // find the relative path based on the call point. For CLI applications) very practical path. join (process. cwd (), 'My-dynamic-file') // or path. resolve ('My-dynamic-file') // find another file path based on one file. join (_ dirname, 'My-package-file ')
Read files
The simplest way to asynchronously read files in a node is to use a stream! The following is an example:
const path = require('path')const fs = require('fs')// read a file and pipe it to the consolefs.createReadStream(path.join(__dirname, 'my-file')) .pipe(process.stdout)
Create a file
It is not difficult to create a file. Here is a cat command implemented by node:
const path = require('path')const fs = require('fs')// cat ./my-file > ./my-other-filefs.createReadStream(path.join(__dirname, 'my-file')) .pipe(fs.createWriteStream(path.join(__dirname, './my-other-file')))
Delete an object
The rm-rf command is usually used to delete files and directories in Shell scripts. A rimraf in NodeJS also implements the same functions:
const rimraf = require('rimraf')const path = require('path')rimraf(path.join(__dirname, './my-directory'), err => { if (err) throw err})
Create directory
Creating a file is similar to deleting a file. Use the mkdirp package.
const mkdirp = require('mkdirp')const path = require('path')mkdirp(path.join(__dirname, 'foo/bar'), err => { if (err) throw err})
Search for files
Use readdirp to find the 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)
Use findup to find the files in the current parent directory:
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 Pipelines)
The error of the entire data stream is handled very once in the pipeline. Instead of using. on ('error', cb) for each separate 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 nowconst rs = fs.createReadStream('./in.file')const ws = fs.createWriteStream('./out.file')pump(rs, ws, err => { if (err) throw err})
Articles you may be interested in:
- Node. js local file operations-file copying and directory traversal methods
- Node. js file operations
- Nodejs File operation module FS (File System) common functions concise summary
- Use Node. js to process the encoding of front-end code files
- Node. js small tools for JS file Merging
- For details about the instance, Nodejs saves the file sent by payload.
- Node. js static file server Ultimate Edition
- Implement File Upload Based on nodejs + express (4.x+)
- Node. js captures and analyzes js files with or without special content on the webpage.
- C # readnodefile () cannot read osg files with names of Chinese Characters
- How does node. js read files to strings?
- How to Use HTTP to upload files in Node. js
- Node. js removes the evil information from the torrent file.
- Solution to 360 false positive trojans when node-webkit is packaged as an exe file
- NodeJS Web application listens to sock file instances
- Modify the file encoding format in batches using nodejs
- Node. js removes BOM headers in batches
- Node. js allows you to find a string and its file in the directory.
- Node. js simulated Browser file upload example