-: In this section, we're gonna write out
all the JavaScript code required
to implement this Node application.
Now remember, if you're here in this course
without a background in Node,
or maybe you're not interested in JavaScript at all,
that's totally fine.
You can skip this section
and go to the next section where I've put all the code
that we're gonna write right now,
that you can just copy paste it over very easily.
So again, if you don't want
to write out a bunch of JavaScript, just continue
into the next section.
Otherwise stick around and we'll get started right now.
All right, so to get started, I'm gonna flip back over
to my Terminal.
You'll notice that I'm again
in a workspace directory of sorts,
so I'm no longer inside that web directory
that we were working in earlier.
Inside of here, we're gonna make a new project folder
and I'm gonna call it, how about just, "Visits," like so.
Because that's what this application is doing,
it's counting some number of visits.
I'm then going to change into that directory
and then I'll open up my code editor inside there.
Now we're going to create two separate files very similarly
to what we did on the last application we worked on.
We're gonna have a package.json file that's going
to record all the dependencies of our application,
and then we're gonna have an index.js file,
which will serve as the actual Node server.
Let's first make the package.json file.
So inside of my route project directory,
I'll make a new file called package.json,
and then inside of here, we're gonna specify
some number of dependencies and a scripts section as well.
So first we'll do our dependencies.
Don't forget that we are gonna use double quotes
everywhere inside this file.
We're going to pull in Express,
and I don't really care what version of Express we use,
so I'll put an asterisk.
And then we're also going to get a second dependency
that we did not use previously called Redis.
Now this Redis dependency right here
is a JavaScript client library for connecting
to a Redis server.
So this library right here is how we are going
to somehow kind of connect over
to our running Redis server, and pull information from it,
and update information inside of it as well.
Now for the Redis library,
we are going to use a specific version.
We're gonna get 2.8.0.
So that's the two dependencies we're going to need.
I'm gonna put a comma in,
and then we're gonna add on a Scripts section as well.
And very much like we had last time,
we'll have a single start script,
and anytime we run that
it'll actually execute Node index.js.
All right, so that's it for our package.json file.
I'm now gonna make a second file,
that's gonna be our index.js file
and that's gonna hold all of our actual application code.
So I'm gonna make a separate file called index.js,
and at the top we're going
to require in two separate libraries.
We're going to get Express again,
and then we're also going to import that Redis library
that we just added as a dependency.
We'll then make a new instance of an Express application,
and at the same time we're also gonna set up a connection
to our Redis server.
So I'll say, "Const client."
So client right here is going
to represent our connection over to the Redis server,
and we'll say, "Redis.createClient," like so.
Now, we're going to come back
and add in a little bit more information
to this create client function call right here.
We're going to eventually specify the host,
or the URL, or the address of the Redis server
that we're trying to connect to, and it's port as well.
But that's going to be a part
of a little bit of conversation
that we'll have down the road,
once we start to actually put together the Docker side
of this application.
After that, we'll add in a route handler
for our root route.
We'll say anytime that someone comes
to the root route of our application,
we're going to call this callback function.
Now inside of here, we're going to attempt
to use this client connection over
to the Redis server to get the current number of times
that our page has been visited.
So to do so I'll say, "Client.get visits,"
and then as a second argument,
we'll add in a callback
that gets called with a possible error object
and the number of visits that are stored inside of Redis,
and then we'll send a response back
to whoever made this original request.
We'll just say, "Number of visits is,"
and then we'll add on through some
simple string concatenation, visits, like so.
After doing so, after sending this response back
to the user, we'll make sure that we also update
the number of times that this page has been visited.
And so to do so,
we're going to again reference our client object.
I'll say, "Client.set visits,"
is going to be visits plus one.
Now a little bit of a gotcha here, this visits variable
that we get back from Redis is actually gonna come back
to us as a string, rather than the integer
that we would really expect it to be.
And so just to make sure that we add one to an integer
and not a string, I'm going to wrap this
with a parseInt function call.
Then the last thing we need to do is add on a app.listen.
We'll again use Port 8081,
and then once the application successfully starts listening,
we'll do a console log that says, "Listening on Port 8081."
Now one last very quick thing that we need
to do inside of here.
You'll notice that we're kind of assuming
that the instant our server starts up,
we've already got some number of visits stored,
or like there's some existing initial value for visits.
That's probably not the case.
So one thing that we'll do
right after we start up our client right here.
we'll make sure that we do a client.set visits to zero.
So this right here is going
to initialize our number of visits to be zero,
and then every time that user comes to our root route,
we'll pull that number out, and then increment it by one,
and store it back on the Redis side.
Okay, so that's it for this file.
That's all the application code that we need to write,
with the exception of a little bit of networking stuff
that we're gonna put inside of here
to make sure that we connect to our Redis server.
So let's take a quick break right here.
We'll continue in the next section,
and we're going to continue working on our Docker setup.