O'Reilly logo

Node.js Web Development - Third Edition by David Herron

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Storing notes in the filesystem

The filesystem is an often overlooked database engine. While filesystems don't have the sort of query features supported by database engines, they are a reliable place to store files. The notes schema is simple enough that the filesystem can easily serve as its data storage layer.

Let's start by adding a function to Note.js:

get JSON() {
    return JSON.stringify({
        key: this.key, title: this.title, body: this.body
    });
}

This is a getter, which means note.JSON (no parenthesis) will simply give us the JSON representation. We'll use this later for writing to JSON files.

Take a peek at the static function mentioned in the following code:

static fromJSON(json) { var data = JSON.parse(json); var note = new Note(data.key, data.title, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required