-: In the last section,
we started putting together our server implementation
by defining the package.js on file
and a couple of keys that are going to be used
to connect to our running Redis instance
and the running PostgreSQL instance, as well.
I'll now continue by creating a new file
inside of the server directory.
I'm going to call this 'index.js.'
And this single file
is going to house all the logic that we need
to connect to Redis, connect to PostgreSQL,
and eventually kind of broker information
between the two of them and the running React application.
So we're going to do a lot of typing in here.
Again, please make sure that you are paying close attention
to all this spelling and all that kind of good stuff.
Because like I've said previously,
it's going to be a little bit of a pain
to trouble to shoot this stuff later on.
All right, so let's get through this.
At the very top of this file,
we're going to first require in all the keys
that we just defined inside the keys.js file.
And then we're going to define
a couple of different sections inside of here
as we're going to have
several different things that are going on inside this file.
The first section that we're going to make is going to be
dedicated to Express app setup.
So this is where we're going to define and set up
the Express side of the application.
So I'm going to first require in the Express library.
I'm going to also require in something called body-parser.
And we're also going to require in CORS.
I'll then make a new Express application
and I'll tell the app to use
CORS and app.use (bodyParser.json ()), as well.
And notice that on both these lines right here
I say CORS, and I've got a set of double parenthesis
and bodyParser.json, and then double parenthesis
there, as well.
Very quickly, let's talk about what's going on right here.
We first require in the Express library.
We require in the body-parser library
and the CORS library, as well.
We then create a new Express application.
So this app right here is essentially the object
that's going to receive and respond to any HTTP requests
that are coming or going back to the React server,
or excuse me, the React application.
We then wire up something called CORS.
CORS is short for Cross Origin Resource Sharing,
and it's essentially going to allow us to make requests
from one domain that the React application
is going to be running on
to a completely different domain,
or different port, in this case,
that the Express API is hosted on.
The body.parser library right here is going to parse
incoming requests from the React application
and turn the body of the post request
into a 'json' value that our Express API
can then very easily work with.
Now very quickly, I think that we might have missed
the dependency body.parser
inside of our package dot json file.
You know, I'm looking at this
and I think that we did miss that.
Let's add on that dependency here very quickly.
So inside of the package.json file
I'm going to add on body-parser
and I'll just specify the version as "Star," like so.
And we'll just get the latest version of it.
Okay, so quick change inside the package.json file.
Now back over inside of index.js.
After doing the Express app setup,
we'll then do some setup to create and connect to
our running PostgreSQL server.
So this will be a new section that we'll call
PostgreSQL client Setup.
So this is going to be all the logic required
to get the Express application to communicate
with the running PostgreSQL server.
Remember, PostgreSQL is a SQL type database
very similar to say MySQL, My SQL.
So right here we're going to require in
the pool module from the PG library.
Notice that I've got the early braces
around pool right here.
And then we're going to create a new PG client
or PostgreSQL client, out of this pool object.
So I'll say 'New Pool' and to this thing
we're going to pass in some of the different
keys that we had defined inside of the keys.js file.
And remember we already required in that keys file
at the very top.
So we're going to pass in 'user' as keys.pg user,
pass in 'host' as keys.pg host,
database as keys.pg database,
password as keys.pg password,
and port as keys.pg port.
Now again, can't say it enough
please double check all the keys on the left hand side
and double check your spelling on PG user,
host, database, password, and port.
Then after all that, we're going to add on
a little air listener down here.
We're going to say PG client.on error.
Anytime that an error with a connection occurs
we'll console log out a little message that says,
"lost PG connection."
Like so.
Okay, so that's it for doing the initial setup
of the PostgreSQL client.
But unfortunately we have to do
one other quick piece here.
Anytime that we connect to a SQL type database,
we have to initially create at least one time
a table that's going to store all of the values.
So in our particular case, we're going to create a table
that's going to store all of the values that have ever been,
or all the indices, to be precise, that have been submitted
to our Fib calculator.
Remember, that's the only job of PostgreSQL here.
PostgreSQL is just going to store the indices
of any submitted value.
It's not going to actually store the calculated values
or anything like that.
It's just saying, "Hey this index of 10, this index of five,
they have both been submitted."
So we're going to create a table inside the database
to house that information.
Now the syntax that we're going to use for that right here
is going to be just a little bit complicated.
We're going to say PG client.query,
and then inside of a string, we'll say,
"create
table
if not exists."
So this is going to create a table
if it has not already been created.
And we'll say values.
And then in a set of parenthesis we'll say
"number hint," like so.
So the name of the table is going to be "values"
and it's going to store a single column of information
that we'll refer to as "number."
And so number right here is essentially going to be
the index of the submitted value from the React application.
Now to this thing, I'm going to go to
leave off the semicolon on the very end
and I'm going to add on a catch statement.
So if anything goes wrong
with creating that table
we'll console log an error.
So that we know, "Hey we were not able to
successfully make the table."
All right, so that's it for the PostgreSQL setup.
We've got all the connection logic right here
and we've got a single line of code right here
that's going to make sure that we've got a table
inside that database that can hold the indices
of all the submitted values that have been entered
into the application.
Let's do another quick break right now.
When come back to the next section, we're going to add on
another little block of configuration down here
that's going to make sure that we can connect to our
Redis instance from the Express API.
So another quick break, and I'll see you in just a minute.