In this chapter we’ve used Node.js to perform file operations in Node.js’s evented, asynchronous, callback-oriented way. You learned how to watch files for changes and to read and write files. You also learned how to spawn child processes and access command-line arguments.
Along the way, we covered the EventEmitter class. We used the on() method to listen for events and handle them in our callback functions. And we used Streams—which are a special kind of EventEmitter—to process data in buffered chunks or pipe it directly to other streams.
Oh, and let’s not forget about errors. You learned Node.js’s convention of passing an err argument to callbacks, and how error events can be captured from an EventEmitter.
Keep these patterns in mind as you continue through the book. Third-party libraries sometimes have different styles, but the concepts you’ve learned here reappear throughout the Node.js ecosystem.
In the next chapter we’ll dig into the other form of server-side I/O: network connections. We’ll explore the domain of networked services, building on the concepts and practices developed here.
The following are some bonus questions for you to try out your newly gained Node.js knowledge.
The various example programs we developed in this chapter lack many safety checks. Consider the following questions, and how you’d change the code to address them:
In an early example of our file-watcher program, we pulled the filename to watch from process.argv. Consider these questions: