The Hard Parts of Servers & Node.js File System Q&A
Transcript from the "File System Q&A" Lesson
>> Speaker 1: Andrew, go ahead my friend.
>> Speaker 2: When we did the JSON that parse, do we also add that execution on that circle stack and strip it back off?
>> Speaker 1: They technically do behind the scenes. But because we didn't write that function and therefore, we don't really have any interest in it been on there and like we're the threat of execution that we could control always going inside of there.
>> Speaker 2: Does it always return a string of phi.json or can you get it in other formats like raw or.
>> Speaker 1: So actually, I don't know this too much here. But actually, the data that comes in from you're talking when the data comes in from the background, it actually comes in and in what's called a buffer format.
[00:01:01] It actually doesn't come in in stringified JSON, until you convert it using JSON parse. So I'm being a little bit sort of cheeky here by saying it comes in as stringified text. It actually technically comes in what’s called a buffer. Which is a very flexible way of storing data, because it’s literally just a series of zeros and ones.
[00:01:27] And it's node’s way of saying here’s generic data. Now, run functions like to string, that will turn it into a stringified version. With .parse, it's alternate to a string and parse it in one step. So technically, it's a very nice clarification from Michael. Technically, the auto inserted data is not actually already pre-stringified.
[00:01:50] I mean, it is stringified here, but when it gets parsed in it gets parsed in as a series of zeroes and ones. Known as a buffer data format and that's actually what gets inserted here. And then when we run JSON parse, we convert it. And so, I'm gonna guess that probably inside our clean tweets function if we're cleaning the tweets, we must have had to stringified there, and then turn it back into JSON.
[00:02:15] I know it's stringified there, and then parse the stringified version when we brought it back out. So it's a nice clarification Michael, it turns out we have a very flexible way of freeing data in of any type. Image data, video data, text data, it all come in in what is called the buffer format which is zeroes to one representation.
>> Speaker 2: And it's a string?
>> Speaker 1: It's technically, that is a collection of zeroes and ones that can be updated and you can take pieces off it and start working on them, and add more pieces into it. We'll see that in a second, we'll see that in a second, I promise very slight, Mohammed?
>> Speaker 2: Two quick points, did I hear you mention in beginning that you have to import efforts?
>> Speaker 1: We absolutely do, always with any of these background features we have to tend if we write require HDP require FS. It's a one line thing to get access to this background feature.
[00:03:08] I hold it off on here, just to keep our code focused on what matters, which is the interaction between this feature. But actually, we would have, let's just make sure no one misses this, actually the line above would say FS =equals require FS, and that's just gonna set up our access to this feature.
[00:03:29] Good addition, Mohammad, yeah, is there a question?
>> Speaker 2: Yeah, the error data, is that come with inherent to the FS function? Or is that just a label that you just named, why, what, how could there be error dater
>> Speaker 1: The error dater that actually gets created by a node.
[00:03:47] What's the error dater that gets created by a node here, because there's no error?
>> Speaker 2: Null.
>> Speaker 1: Null, does that have a label? Does that got a variable name for it?
>> Speaker 2: No.
>> Speaker 1: No, its everything auto-created by a node has no inherent label. You'll see in the docs, it will be given a label which is to me really unhelpful.
>> Speaker 2: Parameters.
>> Speaker 1: Parameters, they're our placeholders that we know will be filled in such that we can access the data that gets filled, what's the parameter name we gave for the auto inserted error data? Mohammad.
>> Speaker 2: Error data?
>> Speaker 1: It's error data, what's the parameter the name that we gave, we could have called it, here's the worst thing, people.
[00:04:38] We could have done this and called it data and we could have called this one error data.
>> Speaker 2: And you'll lose your job.
>> Speaker 1: [LAUGH]
>> Speaker 2: But you could switch them round, node has no idea those are words meaning anything to us. They're just placeholders, that as long as we refer to them inside, so if we were writing our function in full Mohammed, we would write if error data console log error data.
[00:05:09] If error data has a value other than null, so actually, we could just write if error data, because it would default to being falsey, if we have null. If error data is not null, then console log that. But, yeah, error data is just as long as we refer to it inside the function by the same name.
[00:05:27] We could literally be terrible and switch these labels. It'd be horrible, but they're just this, I'm gonna call this one, I'm gonna call the data from now on, Mohammad's,
>> Speaker 1: Data.
>> Speaker 2: That's the non-error one, right?
>> Speaker 1: Yeah, yeah, yeah, you thought I'd write on with the error one.
>> Speaker 1: You own the error, Mohammed, you're gonna be the, no no. Okay, so it's completely arbitrary what we call them. All that matters, quite clearly, is the order and this is known as the error first pattern. Why they have the error, auto create error, information about the error inserted first?
[00:06:08] I do not know, I guess, it's very important, right? But it's the error first pattern where the first bit of data, you can't miss it. You know why they do it, because it means you can't miss it. Everyone otherwise, not bother handling it, the parameter would be just be my data's it, I'm gonna get my data every time.
[00:06:49] Is that go ahead sir?
>> Speaker 2: Yeah, so let's see you actually like, don't find the file or something like that. Then error will be triggered. Would the important Tweets at all, like run, in terms of go look through the file, because it doesn't really have to, right?
>> Speaker 1: That's our job to write code in here saying if error data not null.
[00:07:13] We can just write if error data, because if it's got something in it it's gonna be inherently truthy. And therefore, it'll evaluate if error datas are true, if error data is true, do something else. Handle the error and just write a conditional to handle the error.
>> Speaker 2: But you can also do it the same way that we did it with the server on when you have a little flag that says error, and then it calls a function.
>> Speaker 1: Well ,this is where all this will go and look at the, yes, absolutely. But this is where I draw the sort of documentation and see exactly what type of errors, and what sort of situations will still need this function to run, and what ones would lead event error to trigger another function running.
[00:07:48] And we can go to the documentation to see the specifics of how it's done.