GithubHelp home page GithubHelp logo

ch2-filesystem's Introduction

ch2-filesystem

N8TRW: access a filesystem: reading, writing, renaming, and deleting files.

Since you’ll be touching the target file a lot to trigger changes automatically, you might want to use the watch command to do this automatically:

$ watch -n 1 touch target.txt

Run launch the watcher program using node, like so:

$ node watcher.js

Reading Command-Line Arguments

$ node watcher-argv.js target.txt

Spawning a Child Process

Enhancing our file-watching example program even further by having it spawn a child process in response to a change.

$ node watcher-spawn.js target.txt

Capturing Data from an EventEmitter

Sometimes you’ll want to capture data from a stream, rather than just piping it forward. Let’s see how to do that in capturing data from an EventEmitter

$ node watcher-spawn-parse.js target.txt

Reading and Writing Files Asynchronously

Node.js’s methods for reading and writing files. Along the way we’ll see two common error-handling patterns in Node.js: error events on EventEmitters and err callback arguments. There are a few approaches to reading and writing files in Node. The simplest is to read in or write out the entire file at once. This technique works well for small files. Other approaches read and write by creating Streams or staging content in a Buffer.

Writing Files Asynchronously

$ node write-simple.js

Reading Files Asynchronously

$ node read-simple.js

Creating Read and Write Streams

The cat.js uses a file stream to pipe a file’s data to standard output:

$ chmod +x cat.js
$ ./cat.js target.txt

You can also listen for data events from the file stream instead of calling pipe(). The following program called read-stream.js does this:

read-stream.js no-such-file
$ node 

Example of how to read a file using the readFileSync() method:

const fs = require('fs');
const data = fs.readFileSync('target.txt');
process.stdout.write(data.toString());

ch2-filesystem's People

Contributors

devshittu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.