In this section we're going to 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 you maybe you're not interested
in javascript at all.
That's totally fine.
You can skip the section and go to the next section where I've put all the code that we're going to
write right now.
They could 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 going to flip back over to my Turnell.
You'll notice that I'm in in a workspace directory of sorts.
So I'm no longer inside that web directory that we were working in earlier.
Inside if you're going to make a new project folder and I'm going to call it how about just visit's
like so because that's what this application is doing.
It's counting some number of visits and 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 going to have a package touchiest on file that's going to record all the dependencies of our application
and then we're going to have an index such as file which will serve as the actual notes server.
Let's first make the package not just on file.
So inside of my project directory I'll make a new file called package Jaison and then inside of here
we're going to specify some number of dependencies and a scripts section as well.
So first we'll do our dependencies.
Don't forget that we are going to 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 restis.
Now this read is dependency right here is a javascript client library for connecting to a server.
So this library right here is how we are going to somehow kind of connect over to our running Retta
server and pull information from it and update information inside it as well.
Now for the read us library we are going to use a specific version we're going to get to point eight
point o.
So that's the two dependencies we're going to need.
I'm going to put a comma in and then we're going to add on a script's section as well.
Very much like we had last time.
We'll have a single start script and any time we run that it will actually execute node index Geass.
All right so that's it for our package not just some file.
I'm not going to make a second file that's going to be indexed.
J.S. file and that's going to hold all of our actual application code.
So I'm going to make a separate file called inducts not J S 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 rightest 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 going to set up a connection to our red S server so I'll say Konst client.
So client right here is going to represent our connection over to the Retta server I will say redstart
create clients 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 we like your L or the address of the server that we're
trying to connect to in a sport 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 Dockers side of this application.
After that we in a row handler for a route route will say any time that someone comes to the 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's connection over to the Reddit server
to get the correct number of times that our page has been visited.
So to do so all say client DOT gets visits and then as a second argument we'll add in a callback that
gets calls with a possible air object and the number of visits that are stored inside of read us and
then we'll send a response back to whoever made this original requests we'll just say a number of visits
is and that will add on through some simple string concatenation.
Visits like so
after doing so after sending this response back to the user will 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 or say client set visits is going to
be visits plus one.
Now a little bit of a gotcha here.
This visit's variable that we get back from rattus is actually going to 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 purse int function call.
Then the last thing we need to do is out on a lesson will again use port 80 81 and then once the application
successfully starts listening we'll do a console log that says listening on port 80 81.
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 a user comes to our route we'll pull that number out and then increment it
by one and store it back on the roadside.
OK.
So that's it for this file.
That's all the application code that we need to right.
With the exception of a little bit of networking stuff there we're going to put in outside of here to
make sure we connect to our server.
So let's take a quick break right here.
We'll continue in the next section.
Now we're going to continue working on our docker setup.