We are all done with the first goal of our API, so we are correctly serving up Riak project files.
Now going to move on to steps two and three, which are essentially to retrieve a list of cells out
of a stored file and add a list of cells into an existing file as well.
These are going to be pretty easy and straightforward routes to put together.
Really just going to receive some requests, pull some information out of the request, and then save
some data into a file using the first module, which is a node standard library module.
So let's get to it back inside my editor.
I'm going to find my local API project inside.
There, I'll find the Sarsae directory and I'll make a new folder inside there called Route's.
Then inside of roots, I'll make a new file called Cels Dots.
Inside of here, I'm going to first express.
From Xpress.
I'll then create an express router.
If you're not familiar with the router, it is extremely similar to the app object that we're currently
working with inside of our index not file.
So that express app right there.
So a router has methods assigned to it, like get post, delete does used to wire middleware and so
on.
So we're going to add a couple of routes onto this router object and then merge this router back into
our app over inside of our indexed dots file.
So we will first set up one or two different routes.
Let's do a router dot get cells, we'll have that B async await.
And then a function to run and we'll do a router post two cells that will also be an async function.
OK, looks good.
Let's receive our request in response objects on both them.
And then inside of both these, let's just write a couple of comments out just to guide ourselves and
make sure that we can kind of consider a couple of different scenarios to first off, in the case of
getting a list of cells.
The first thing we probably need to do inside of here is make sure that the file that is going to store
a list of cells actually exists.
So we're going to make sure the cell storage file exists.
Remember, whenever a user starts up our supply, they're going to optionally provide us the name of
the file that they want to use for storing cells if that file doesn't exist.
We should probably create it for our users.
So step one right here is to just make sure that file exists.
If it does not, we'll create it for our users and maybe put in some default content into it as well.
Let's put in a comment for that as well.
So I'll put in if the file or if it does not exist, add in a default list of cells.
And after that, once we are 100 percent sure that this file exists, we will read the file.
Here's a list of cells out of it.
And then finally send list of cells back to Browsr.
That's pretty much it should be pretty straightforward.
Our post method down here will be pretty straightforward as well.
So in this case.
We probably want to just make sure once again that this file exists.
It is entirely possible that a user might delete the file in between save attempts or maybe rename it
or something like that, so we might want to make sure that the file exists.
And if it does not.
We will create it.
After that, we're going to take the list of cells from the request object because remember, our assumption
here is that a user is going to be saving or sending us a list of cells that they want to persist.
We're going to take the list of cells from the request object and we're going to serialize them or in
other words, turn them into a format that can be safely written into that file, will then write the
cells.
Into the file.
And that's pretty much it.
So both these handlers are going to be pretty straightforward in nature, and the one thing I want to
mention here that's just a little bit not standard.
Remember, back inside of our inducts file, our search function has been given the file name and the
directory that file citzen.
So we really need to somehow communicate these two variables over to this cels router.
So to do so, we're going to wrap up all this router creation stuff and all the router configuration
in a function.
And we're going to make sure that that function will eventually be coupled with the filename and directory
that should be used for all this file related stuff.
So right above construed right here, we're going to add in and export to create sales Rohter.
I'll then place the closing curly brace on the very bottom line of the file.
So all this stuff is going to go into the body of that function.
Well, then make sure that we provide the file name as a string and the directory name as a string.
So then finally, let's go back over to our next thoughts while we're going to import cells, Rueter,
we're going to create the router and when we do, we'll make sure we pass in our filename and directory
will then make sure we return the router at the bottom of this function.
Let's do that very quickly.
So down here, I'm going to return the router and we'll make sure we wire that up to our express app.
Let's go back over to index at the very top, we will import create cells, rueter from route's cells.
And then right above our existing return statement will do an app not use, that's how we wire up an
express router, create sells router, we're going to call that function and we'll make sure we provide
the filename and the directory and remember file name and directory arguments to our search function.
They are right there.
OK, so it looks good now all you have to do is a pretty good amount of work around these two different
handlers and we should be all set.