Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Importing with fs" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will explains location in the terminal, the benefits of using json to store data, and then diagrams the process of storing and using json data.


Transcript from the "Importing with fs" Lesson

>> Will Sentance: For now, let's just get importing our tweets with the help of fs. And go to function equal cleanTweets. We're not gonna talk about how it works. But just imagine it's a simple algorithm that goes through, maybe it's a list of mean words. And it's just gonna go through and check each of the words in our string of tweets, massive cluttery, and just delete them, at its simplest.

Take the 1.5 gigabytes down to maybe 70 megabytes, so a joke for you.
>> Will Sentance: That's not actually true. All right, okay, so then we go to function useImportedTweets. And then we're gonna use fs.readFile, which is gonna be a label for the Node C++ feature fs that gives access to the file system.

And we're gonna parse it, just a string of characters. To JavaScript, that's just a string of letters. It means nothing to it. But I have a feeling it's gonna be parsed to Node's background file system access feature that has the ability to parse, interpret that string of characters.

And use it to resolve to an actual file position in the internal computer features, in the file system, in the hard drive. Every single file in your hard drive has a path, a position in its folder. It's in a folder, which is in a folder, which is in a folder.

We use slashes to go into folders. We use dots to indicate look in the current folder. What is the current folder? The current folder is wherever you chose to turn Node on and what JavaScript file you were running in. It's not literally where the JavaScript file is saved.

And we said we saved this JavaScript file and turned it on. It's not literally where it's saved. Whenever you open Node, you open it from the terminal inside of specific location in your file system. You're in the prompt in the terminal. The prompt is a little bit before you start typing.

It's always got a position, a location you're opening, you're running that app in. Wherever you ran Node, that will be the current folder location. So if you see the dot, Node's gonna look first in the folder in which you switch Node on in, in which you're running the app Node within.

You always run any app through the terminal in a particular location. In a terminal, you never just run them on your computer, you're always in a folder. So where that folder is, that's where you start off looking. And you do slash to say, look in this existing folder for a file called tweets.json.

What is this .json thing?
>> Will Sentance: It's a really, really, really useful way of storing data. In JavaScript, people, we love objects. They're really easy to break to go down multiple areas and find the exact bit of data we want in it. It really organize data. But if I want to store that object, and it's an object for the tweets.

Let's say I make an object for the tweets,
>> Will Sentance: With ton of tweets in here. If I wanna store that object outside of JavaScript, I can't cuz it's a JavaScript construct that only exists and work through my JavaScript memory. It's not like a string of characters that I can store anywhere.

So if I wanna store it in my file system long term, I need to convert it into a kind of version of an object that is just like a string of all the properties of an object. So if had an object like, I don't know,
>> Will Sentance: I don't know, tweet 1 is yo, tweet 2 is hi, then I could convert this into a string with each property,

>> Will Sentance: Tweet 2, I could convert into a string of characters. If there were one of these with another object, I'd open it out and then save that as a .json file. It's just text, just a string of characters. I could then load that stringified, turned into a string, version of my JavaScript object back into JavaScript and run a function called, well, what function?

Raise your hand if you know, Seph.
>> Seph: Parse.
>> Will Sentance: JSON.parse that will turn that stringified version of my JavaScript object back into a workable JavaScript object that I can use. It's a way of long-term archiving, I guess, preserving JavaScript objects in a way that can then be parsed around from computer to computer.

I can't parse a JavaScript object that only exists in JavaScript. I can't parse that construct into the file system over the Internet to another computer. I have to turn it into something which is just a string of characters. It's a stringified version of a JavaScript object, which is exactly what we need.

Okay, so we're gonna have our tweets stored in the archive in that format, and we're gonna bring them back in. And I guess we're gonna run JSON.parse on them to convert them back into JavaScript object, which we can use, and clean them in the process. Okay, here we go, people.

For the first time, we're using another Node, and therefore, computer internal feature besides its networking ability. It's the file system or file storage feature. Okay, here we go. With my wipe down, I'm gonna leave up all our framework, our overall architecture of Node. We're using, people, a new feature of our computer's internals, the file storage.

File storage or file system is the organizing structure of the file storage. There it is. And now, I presume we must have saved a file. What's the file we must have saved, Matt?
>> Matt: tweets.json?
>> Will Sentance: Yeah, tweets.json, yeah, tweets.json. I presume we saved it in the same folder.

>> Will Sentance: Same folder, you're less impressed than I am on my folder drawing. Same folder as we're running Node in, same location, and it's tweets.json and it's a file. There it is. That's pretty good, right? Yeah, thank you William. It's that all, by the way throwback to Windows 3.1 UI.

Wow, through our over 30s in the building. All right, I turned 33 four months ago, in a good way and I'm very happy by it. Life's on track. No, life's on track, life's on track. Life's even good, it's good. I tell you, I just hate when people say 30 and then they say that annoying phrase.

>> Will Sentance: No, they say 30, I don't like this. I think it's like, how insecure must you be about turning 30, they have to bring it up. And so anyway, moving on.
>> class: [LAUGH]
>> Will Sentance: All right, so it's all set up. But do we have access to the file storage system through JavaScript?

And one thing we do, definitely not. In the web browser, we definitely not either for security reasons. So from the start, it was not looking good to have access. Thank God we had someone write a ton of C++ code to give us access. And of course, the wonderful team, I ain't gonna shout them out, at libUV who worked in conjunction, but they are a big part of the interface between these two portions, especially when it comes to accessing the file system or file storage.

Because actually, we're gonna see libUV is gonna spin up a thread of execution in the background to handle the pulling of that data into Node. Just because the crate is discovered, there was too much complexity and too many risks with relying on using the computer internal operating system ability to basically spin up background threads or handle things going on in the background and said, okay, we're just gonna always make sure we have an open focus channel to pull this data into Node.

We'll talk more about that a little bit later.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now