The Hard Parts of Servers & Node.js Importing with fs
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.
[00:00:20] 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.
[00:01:02] 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.
[00:01:53] 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.
[00:02:12] 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.
[00:02:30] What is this .json thing?
[00:02:52] Let's say I make an object for the tweets,
[00:03:10] 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,
[00:04:11] Raise your hand if you know, Seph.
>> Seph: Parse.
[00:05:04] 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.
[00:05:35] 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.
[00:06:24] 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]
[00:07:07] 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.
[00:07:38] 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.
[00:08:10] We'll talk more about that a little bit later.