Node.js File Systems Module

Today

Working with servers

Working with the file system using Node.js

Node.js comes with a builtin file system module to work with a computer’s file system, or for example on a server. It can read files, create files, edit files, delete files, and rename files.

To use it, we must import it.

In Node.js we do this with the require statement.

let fs = require('fs'); 

Note: you may also see this written as const fs = require('fs');

Reading a file

We can read from a file saved on a server. This can be a text file or a json data file for example. We use the readfile module, part of fs.

fs.readFile()

Example

student.json file:

{
  "name": "Layla",
  "age": 22,
  "department": "Math and Computer Science"
}

script.js file

let fs = require('fs');

fs.readFile('student.json', function(err,data) {  
    if (err) throw err;
    let student = JSON.parse(data);
    console.log(student);
});

console.log('This is after the read call.'); 

These two files are in the same directory. We can run our script with node script.js

Doing so will print out:

This is after the read call.
{ name: 'Layla',
age: 22,
department: 'Math and computer Science' }

Since Javascript is asynchronous, we start reading the student.json file, and we have a callback function to run when finished. In the meantime, we print to the console in the last line of our script. Our readFile finally finishes and triggers the anonymous callback function to run that parses our student json data and prints it out.

Writing to a file

You can create a new text file in the file system with fs.writeFile(). If the file already exists, it will overwire the previous content.

You can also add text to the end of a file using fs.appendFile(). This has the advantage of not overwriting a file, and if it doesn’t already exist, it will create the file first.

let fs = require('fs');

let myText = "This text and the random number "+Math.random()*100+" will be added to the end of the file. ";

fs.appendFile('textFile.txt', myText, function (err) {
  if (err) throw err;
  console.log('Saved to textFile.txt!');
});

Again, we can save this as a script file and run it with node script.js or whatever we’ve named the file.

Resources

Example unique web apps

In-class studio time

Final Project

Building on the work you did conceptualizing and building a weather application in the past few weeks use your semester of experience to build a compelling radio streaming app that is uniquely your own. It should work on desktop and mobile. The application should be a unique and compellingly designed radio player that presents the user with a number of stations to select from. Selecting a station should cause the station to begin playing, and any previously selected station should stop. Beyond this, you may add any additional details and design elements you require.

You can find radio station’s sometimes secret audio streams to integrate in your application from Radio Browser.

Just as students each created their own unique weather application, make your own radio streaming app unique to you! 

Requirements:

Bonus (optional) goal:

Final projects must be uploaded to the web by Wednesday May 6 at 11:59.