1
00:00:00,950 --> 00:00:06,160
In this section we're going to write out all the javascript code required to implement this node application.

2
00:00:06,170 --> 00:00:11,000
Now remember if you're here in this course without a background in node or you maybe you're not interested

3
00:00:11,000 --> 00:00:12,070
in javascript at all.

4
00:00:12,110 --> 00:00:13,340
That's totally fine.

5
00:00:13,340 --> 00:00:17,660
You can skip the section and go to the next section where I've put all the code that we're going to

6
00:00:17,660 --> 00:00:18,980
write right now.

7
00:00:19,080 --> 00:00:21,560
They could just copy paste it over very easily.

8
00:00:21,560 --> 00:00:26,650
So again if you don't want to write out a bunch of javascript just continue into the next section.

9
00:00:26,780 --> 00:00:30,030
Otherwise stick around and we'll get started right now.

10
00:00:30,680 --> 00:00:31,000
All right.

11
00:00:31,010 --> 00:00:34,500
So to get started I'm going to flip back over to my Turnell.

12
00:00:34,670 --> 00:00:37,940
You'll notice that I'm in in a workspace directory of sorts.

13
00:00:37,940 --> 00:00:42,170
So I'm no longer inside that web directory that we were working in earlier.

14
00:00:42,390 --> 00:00:47,840
Inside if you're going to make a new project folder and I'm going to call it how about just visit's

15
00:00:47,960 --> 00:00:50,320
like so because that's what this application is doing.

16
00:00:50,420 --> 00:00:56,720
It's counting some number of visits and then going to change into that directory and then I'll open

17
00:00:56,720 --> 00:01:01,190
up my code editor inside there.

18
00:01:01,260 --> 00:01:06,420
Now we're going to create two separate files very similarly to what we did on the last application we

19
00:01:06,420 --> 00:01:07,230
worked on.

20
00:01:07,290 --> 00:01:11,940
We're going to have a package touchiest on file that's going to record all the dependencies of our application

21
00:01:12,240 --> 00:01:16,850
and then we're going to have an index such as file which will serve as the actual notes server.

22
00:01:17,220 --> 00:01:19,920
Let's first make the package not just on file.

23
00:01:19,950 --> 00:01:27,670
So inside of my project directory I'll make a new file called package Jaison and then inside of here

24
00:01:27,760 --> 00:01:33,400
we're going to specify some number of dependencies and a scripts section as well.

25
00:01:33,400 --> 00:01:35,570
So first we'll do our dependencies.

26
00:01:35,920 --> 00:01:41,580
Don't forget that we are going to use double quotes everywhere inside this file We're going to pull

27
00:01:41,580 --> 00:01:45,610
in Express and I don't really care what version of Express we use.

28
00:01:45,630 --> 00:01:51,570
So I'll put an asterisk and then we're also going to get a second dependency that we did not use previously

29
00:01:51,570 --> 00:01:53,060
called restis.

30
00:01:53,280 --> 00:01:59,710
Now this read is dependency right here is a javascript client library for connecting to a server.

31
00:01:59,760 --> 00:02:04,830
So this library right here is how we are going to somehow kind of connect over to our running Retta

32
00:02:04,830 --> 00:02:10,580
server and pull information from it and update information inside it as well.

33
00:02:10,580 --> 00:02:15,930
Now for the read us library we are going to use a specific version we're going to get to point eight

34
00:02:16,020 --> 00:02:18,980
point o.

35
00:02:19,060 --> 00:02:21,780
So that's the two dependencies we're going to need.

36
00:02:21,860 --> 00:02:28,490
I'm going to put a comma in and then we're going to add on a script's section as well.

37
00:02:29,520 --> 00:02:31,370
Very much like we had last time.

38
00:02:31,410 --> 00:02:38,980
We'll have a single start script and any time we run that it will actually execute node index Geass.

39
00:02:39,040 --> 00:02:42,190
All right so that's it for our package not just some file.

40
00:02:42,190 --> 00:02:46,410
I'm not going to make a second file that's going to be indexed.

41
00:02:46,410 --> 00:02:49,940
J.S. file and that's going to hold all of our actual application code.

42
00:02:51,260 --> 00:02:56,070
So I'm going to make a separate file called inducts not J S at the top.

43
00:02:56,070 --> 00:02:59,640
We're going to require in two separate libraries.

44
00:02:59,640 --> 00:03:08,310
We're going to get express again and then we're also going to import that rightest library that we just

45
00:03:08,310 --> 00:03:09,950
added as a dependency.

46
00:03:13,240 --> 00:03:16,380
We'll then make a new instance of an express application.

47
00:03:16,750 --> 00:03:22,880
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.

48
00:03:22,880 --> 00:03:29,060
So client right here is going to represent our connection over to the Retta server I will say redstart

49
00:03:29,100 --> 00:03:35,220
create clients like so now we're going to come back and add in a little bit more information to this

50
00:03:35,280 --> 00:03:37,580
create client function call right here.

51
00:03:37,800 --> 00:03:43,080
We're going to eventually specify the host or we like your L or the address of the server that we're

52
00:03:43,080 --> 00:03:45,480
trying to connect to in a sport as well.

53
00:03:45,720 --> 00:03:50,400
But that's going to be a part of a little bit of conversation that we'll have down the road once we

54
00:03:50,400 --> 00:03:53,460
start to actually put together the Dockers side of this application.

55
00:03:55,300 --> 00:04:01,300
After that we in a row handler for a route route will say any time that someone comes to the route of

56
00:04:01,300 --> 00:04:08,410
our application we're going to call this callback function.

57
00:04:08,500 --> 00:04:13,510
Now inside of here we're going to attempt to use this client's connection over to the Reddit server

58
00:04:13,660 --> 00:04:17,670
to get the correct number of times that our page has been visited.

59
00:04:17,710 --> 00:04:25,660
So to do so all say client DOT gets visits and then as a second argument we'll add in a callback that

60
00:04:25,660 --> 00:04:33,670
gets calls with a possible air object and the number of visits that are stored inside of read us and

61
00:04:33,670 --> 00:04:39,490
then we'll send a response back to whoever made this original requests we'll just say a number of visits

62
00:04:40,820 --> 00:04:45,560
is and that will add on through some simple string concatenation.

63
00:04:46,350 --> 00:04:47,490
Visits like so

64
00:04:50,400 --> 00:04:55,860
after doing so after sending this response back to the user will make sure that we also update the number

65
00:04:55,860 --> 00:04:58,440
of times that this page has been visited.

66
00:04:58,530 --> 00:05:05,880
And so to do so we're going to again reference our client object or say client set visits is going to

67
00:05:05,880 --> 00:05:08,510
be visits plus one.

68
00:05:08,640 --> 00:05:10,450
Now a little bit of a gotcha here.

69
00:05:10,530 --> 00:05:15,720
This visit's variable that we get back from rattus is actually going to come back to us as a string

70
00:05:15,780 --> 00:05:19,010
rather than the integer that we would really expect it to be.

71
00:05:19,060 --> 00:05:24,600
And so just to make sure that we add one to an integer and not a string I'm going to wrap this with

72
00:05:24,600 --> 00:05:28,440
a purse int function call.

73
00:05:28,630 --> 00:05:37,030
Then the last thing we need to do is out on a lesson will again use port 80 81 and then once the application

74
00:05:37,030 --> 00:05:46,750
successfully starts listening we'll do a console log that says listening on port 80 81.

75
00:05:47,170 --> 00:05:50,240
Now one last very quick thing that we need to do inside of here.

76
00:05:50,280 --> 00:05:55,470
You'll notice that we're kind of assuming that the instant our server starts up we've already got some

77
00:05:55,530 --> 00:05:57,060
number of visits stored.

78
00:05:57,060 --> 00:06:01,830
Or like there's some existing initial value for visits that's probably not the case.

79
00:06:01,830 --> 00:06:07,620
So one thing that we'll do right after we start up our client right here we'll make sure that we do

80
00:06:07,650 --> 00:06:11,610
a client set visits to zero.

81
00:06:11,610 --> 00:06:15,680
So this right here is going to initialize our number of visits to be zero.

82
00:06:15,870 --> 00:06:20,990
And then every time that a user comes to our route we'll pull that number out and then increment it

83
00:06:21,000 --> 00:06:23,130
by one and store it back on the roadside.

84
00:06:24,100 --> 00:06:24,470
OK.

85
00:06:24,490 --> 00:06:26,070
So that's it for this file.

86
00:06:26,170 --> 00:06:28,440
That's all the application code that we need to right.

87
00:06:28,510 --> 00:06:33,160
With the exception of a little bit of networking stuff there we're going to put in outside of here to

88
00:06:33,160 --> 00:06:35,500
make sure we connect to our server.

89
00:06:35,500 --> 00:06:36,960
So let's take a quick break right here.

90
00:06:36,970 --> 00:06:38,250
We'll continue in the next section.

91
00:06:38,270 --> 00:06:40,490
Now we're going to continue working on our docker setup.
