-: In the last section,
we added in some logic to the express server
that's going to get us a connection over
to Postgres over here,
and create a new table inside the database
if it doesn't already exist.
We're now gonna make sure that the express server
has a connection over to Redis.
So anytime that user submits a new number
to the React Application.
React App will push that index over to the express server,
and the express server will then push
that index into the running Redis instance.
So back inside of my index dot js file,
inside of the server directory.
Underneath all the Postgres stuff,
I'll put on a new section that we'll call,
`Redis client setup.
So here we'll say, const Redis is require Redis,
and we're going to do some very similar,
create client calls, that we did inside
of the worker folder just a moment ago.
We'll say, const Redis client is
Redis dot create client.
I'm gonna pass this an object
that's gonna have a host coming from keys dot Redis host.
A port from keys dot Redis port,
and then finally we'll define a retry strategy.
That's gonna be an arrow function that always
returns the number 1000 to say
if we ever lose connection to Redis
try to reconnect to it once every one second.
One thing I wanna point out here is that the key
for retry strategy is separated by a underscore right there
as opposed to the JavaScript standard of Camel case like so.
So make sure that you've got underscore strategy
and strategies should have a lowercase s.
And then very similar to what we did before
where we created a duplicate connection
I'm going to also make a Redis publisher.
I'm gonna use a more descriptive name this time around
then when we created that duplicate copy of the Redis client
over inside of the worker index dot js file.
Just because we have many more variables inside
of this server file.
So I'll say const Redis publisher
will be Redis client
dot duplicate.
Now in case you're curious
we are making these duplicate connections
in both files because according
to the Redis documentation for this JavaScript library
if we ever have a client that's listening
or publishing information on Redis
we have to make a duplicate connection
because when a connection is turned
into a connection that's going to listen
or subscribe or publish information
it cannot be used for other purposes.
So that's why we're doing this duplicate thing
in both locations.
All right, so that's pretty much it
for the initial setup here.
Now we're going to start to define some
of the different routes that our server is going to
eventually respond to.
So down here we'll put
down another section and we'll say express route handlers.
I'm gonna first make a test route
something that we can use to make sure
that our application is working the way we expect.
I'll say app dot GIT forward slash.
This thing is going to be called
with a request and response.
Anytime someone makes a request to the route route
of our express application, I'm gonna send back a response
of hi
like so
we'll then define another route handler
at the route values slash all.
This route handler is going to be used
to query or running Postgres instance and retrieve all
of the different values that have ever been submitted
to Postgres.
So this is essentially going to return all
of the values that have ever been submitted
to our application
or all the indices that have been submitted.
This is going to be an async arrow function
so don't forget the aysync keyword right there.
We'll do request and response,
and then inside of here
we're gonna make a query to the Postgres instance
by saying Const values is a weight
e.g client dot query.
And the query we're going to issue is going to
be a sequel query.
We'll say select star from values.
So essentially look at the values table
that we created a little bit ago
and pull all the information out of it.
And then we're gonna send all that information back
to whoever is making request to this route.
So I'll say res dot send values dot rows.
The dot rows right here is gonna make sure that
we only send back the actual information that we retrieve
from the database and no other information that is contained
inside this values object.
The other information that's in here is some information
about the query itself, like say, how long it took
or what tables we touched or stuff like that.
So we're just gonna send back the relevant information
to whoever made a request to this route handler.
Next up we're gonna add another GIT Request handler
to value values slash current.
This is going to be another async function,
so don't forget the async keyword.
We'll do rack res and inside of here
we're gonna reach into Redis this time.
So reach into Redis and we're going to retrieve all
of the different values, all the different indices
and calculated values that have ever been submitted
to our backend.
So find all the value
or all the indices that have been requested from our users
and return all the accompanying values that have
been calculated for each one.
So inside of here I'll look at Redis client HGETALL
which essentially means look
at a hash value inside the Redis instance
and just get all the information from it.
The hash that we're gonna look at is called values.
We're going to pass in a callback function that has
air and values
inside this thing we're just gonna
send back res dot send values.
Now, quick note here, you'll notice that up here
we're making use of the Await or Async Await syntax
and down here we're doing a very classic callback
for our asynchronous route data handling.
Unfortunately, the Redis library
for note js doesn't have out the box promise support
which is why we have to use callbacks
as opposed to making use of the nice async await syntax.
Just a quick side note there.
All right, so I know this is a long section
but we just need to do one more route handler down here.
So let's just get that done with.
So that last route handler that we're going to
define is going to receive new values
from the React application.
So anytime that a user puts a new index in here
like say 7, and then clicks on submit
they're gonna try to post that information to our back end.
So from the React application to the express server
that's the route handler that we're gonna define right now.
Last route handler.
So I'll say app dot post
because this time we're going to be listening
for a post request,
and I wanna watch
for a post request to the values endpoint.
We'll then put in an aysync callback, so rec res.
And then inside of here
we're gonna first get the index that the user just submitted
from rec dot body dot value, excuse me, index.
So look at the index
that the user just submitted from rec dot body dot index.
We're then gonna make sure
that the index that was submitted is less than 40.
So if a user puts in a number that's
or an index that's really high,
calculating a Fibonacci number
with a method that we've put together inside
of our worker process
for a very large index will take a very, very
very long time, like
on the order of years or decades or centuries.
So we're just going to arbitrarily cap the size
of the index that the user submit to make sure
that our worker process does not just
get arbitrarily forever locked down for all time.
So I'll say if our sent of index is greater than 40
then we're going to immediately return
and send back a status of 4 22
and send the message value or index to high like so.
So now that we've got this index or the number
that we're going to use to calculate a Fibonacci value
we're gonna first take that value
and we're gonna put it into our Redis data store.
So I'll say Redis client dot h set
values
we'll put in the index, and then
as the value for that index, we're gonna put in nothing yet.
So eventually the worker is gonna come to the hash or kind
of data structure that we're creating inside of Redis
and it's going to replace this nothing
yet string with the actual calculated value.
So nothing yet essentially means we've not yet
calculated a Fibonacci value for this particular index.
After we put the value in there
we'll look at the Redis publisher
and we'll publish a new insert event
of that index.
So this right here is gonna be the message that gets sent
over to that worker process.
It's gonna wake up the worker process and say,
hey, it's time to pull a new value out of Redis
and start calculating the Fibonacci value for it.
And then after doing both those steps
we're also going to look
at our Postgres client and we're going to add
in the new index that was just submitted.
So remember, the Postgres database is going to
be storing this kind of permanent record right here.
It's gonna be storing the permanent record
of all the different indices that have ever been submitted
to our application.
So we'll say pg client dot query
insert into values.
We are inserting a value for the number column
so I'm gonna say number
and then values dollar sign 1 like so.
I'll then put in a comma, an array
and I'll list out the index that was submitted
to our application for that.
So this line right here is gonna take the submitted index
and permanently store it inside of Postgres.
And then finally, as a response at the very end
I'll say res dot send.
And we'll just send back some arbitrary response
like working true to say.
All right
we are doing some work to calculate your Fibonacci value.
So now as the very last step inside of the index dot js file
we're gonna set up app dot listen
and we're gonna listen to port 5000 inside of here
and we'll set up a callback and we'll just say something
like console log listening like so.
All right, so I know that's a lot of typing
but that's all we have to do
for the index dot js file and side of our express folder.
So again, we're gonna try executing this file
at the command line.
Now the file's not gonna work as it stands right now
because we do not have a running Redis server
or a running Postgres server, but it's going
to at least allow you to determine
whether or not you have a typo inside this file.
So I'm back inside of the complex directory
which recall is the root folder for our entire project.
I'm gonna change into the server directory, and then inside
of here, I will run node index dot js.
Now you want to see a message of something
like cannot find module express.
If you see anything else, that means that you have a typo
somewhere either inside of the index dot js file
or inside of the keys dot js file.
So you'll want to look at both these files and
try to find the typo.
Now, if you can't find the typo, that's totally fine.
Remember, I'm gonna post all of the correct code that we're
writing right now in like one or two or three more videos.
So if you see an issue, don't sweat it.
You don't have to stare at your code all day.
You can always just open up my copy
of this project and copy paste everything
from my index dot js file into your index dot js file.
Okay, so let's take a quick pause right here
and the next section, I think
that we've got just like a tiny bit more.
Actually, you know what?
I think that might be it.
Oh, we gotta do the React application.
Of course.
How could I forget?
All right, so quick pause, come back in just a second
and we'll put the React app together.
So I'll see you in just a bit.