Working with Files in Node.js
- Using exists()
- File Statistics
- Using open()
- Using read()
- Using writeFile()
- Using close()
- Conclusion
This article discusses handling files from the file system, which is important for loading and parsing files in your application. The file system is big part of any application that needs to handle files for loading, manipulating, or serving data.
Node.js provides some helper properties and methods for working with files (discussed in the sections that follow). Your application may need to know certain attributes about your files[md]for example, whether they are read-only. Most other languages also have these convenience methods, but Node.js may have a few that you might not have seen with other languages.
Using exists()
To determine whether a file exists, use the exists() method of the fs module. Simply pass it the file path and the result is returned as a Boolean value:
var fs = require("fs"); var path = "C:\\Python27\\ArcGIS10.2\\python.exe"; fs.exists(path, function(exists) { console.log(path + " exists? " + exists); });