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

In-class studio time

Final Project

This semester we have built websites for our class, false or misleading institutional websites, personalized clock programs, custom web extensions that modify the browser, an experimental weather web application.

We’ve covered ‘frontend’ programming including HTML, CSS and Javascript, modifying the DOM, as well as working with servers on the backend, creating and deploying node-based web applications with interaction, data persistence and data/API queries.

For this assignment you will make a custom web application, either your own idea, or building from the radio program we began in class.

Goals:

Final projects must be uploaded to the web by Wednesday May 15 at 3pm. Project presentations will be in class on May 8 3PM. We will have a series of smaller steps to accomplish leading up to that.

Example unique web apps

Homework

Get a first prototype ready!

Write a development log of half a page.