Let's get to work on the post method for our cells router, so to get started, we're going to first
import in the first module at the top of this file.
So at the very top, I'm going to import FHS from promises.
And remember, the first module that is included in the node standard library is all about saving files
or loading files on the hard drive.
The default module all operates using callback functions.
So there's a lot of functions inside there.
They all involve some amount of asynchronous code and that a synchronicity is handled using callback
functions.
In general, you and I do not really like callback functions, so we are using a sub module called Promises.
This is the exact same kind of module, but all these different functions inside of your return promises
which will allow us to write out async await code rather than relying upon Nasti callbacks.
OK, so let's then go back down to our post handler right here.
Now, when we were discussing just a moment ago the fact we might need to make sure a file exists and
if it didn't, to create it, that's actually not technically created.
And I probably shouldn't have put that comment inside there.
That was my mistake.
Whenever we write a file to the hard drive using this function inside the module that you and I are
going to use, if that file doesn't exist, it will be created for us automatically.
So I'm just going to take that comment out.
It's something we don't really care about.
So then next up, we're going to take care of the first coming right here, so we want to reach into
our request object.
We're going to assume that there is a list of cells inside there and we need to somehow take that list
of cells and then turn them into some format that can be safely written into that file.
So for that, let's assume that there is maybe a cels property on record body, so I'm just absolutely
assuming that we are going to have a cels property.
Remember, we currently do not have any code inside of a reactor application related to making requests
off to this API.
So we're kind of just setting ourselves up right here, just something we need to remember when we eventually
go back over to our react up and start to send the list of cells off.
And a list of cells is going to be an array of objects for each object is going to have an ID, a content
property and remember a type as well.
And type will be either text or code.
We might want to apply some kind of type information to cells right here just to help TypeScript understand
what we are really working with.
So at the top of this file, I'm going to add in an interface of type cell, and I'm going to say that
this is going to be something that has an ID, that is a string.
Content, that is a string.
And type, that will be either text or code.
Then back down inside of our post method, I'm going to annotate the type of cells with a colon right
there, cells is a array of cells.
Now, if we mouseover cells, we should be told, yep, it is an array of cells.
Now, that is an array of objects, so now we can probably just take that array of objects, maybe turn
it into a string using JSON String Afie and then write that into the file.
And remember, the file we are talking about right now is coming from that file name in that directory
right there.
So to actually write this data into a file, we would do a wait for DOT.
Right file we then put in the path to the file.
We want to write to so in this case, it's going to be a combination of the file name and the directory.
Just to make sure that we form up this entire path correctly, I'm going to import the path module at
the very top.
And then right above all of our router stuff or the router market in the post, I'm going to add in.
Full path right here, and that will be join directory and file name.
So then back down here, we'll do a write file to Full Path.
We want to write into that JSON string of ifI cells.
And we're going to put on a third argument here to just describe the type of information or how we want
this data written into the file.
I want to encoded as UTF eight, which pretty much just means plain text.
So after that, we can go ahead and send back a response and say, hey, everything went successfully,
we're not going to add in any air handling just yet.
But of course, we might want to eventually we'll do a resort send with maybe a status, OK, or something
similar to that.
OK, so pretty easy to get started.
Let's say this right here.
Come back in just a moment, we'll start to take care of the get to slash cells and then eventually
we'll go back over to a react application and make some requests to these two different root handlers.