WEBVTT

00:00.150 --> 00:01.360
Okay well come back.

00:01.560 --> 00:07.990
So in the last section we saw the syntax to add a single route to an express app our app has one.

00:08.010 --> 00:10.870
You are all that you can request to get a response.

00:10.890 --> 00:15.010
Everything else we get that error message that says cannot get.

00:15.020 --> 00:17.890
And then what if you tried to get whatever you tried to request.

00:18.030 --> 00:19.950
So we'll try fixing that now.

00:20.490 --> 00:22.680
We're going to add some more routes.

00:22.800 --> 00:24.260
So the first one will do.

00:24.420 --> 00:26.440
It's going to be silly it's a joke route.

00:26.610 --> 00:32.670
When you go to slash joke it just gives you a joke rather than just doing hello or high or goodbye or

00:32.670 --> 00:33.600
doing a joke.

00:33.600 --> 00:38.900
So when we request from a computer or from a phone even to or you are.

00:39.270 --> 00:43.340
And then we have our express app that we're requesting this time.

00:43.490 --> 00:47.590
Our request is hey I would like the slash joke page.

00:47.840 --> 00:50.910
So that's like this rather than just hitting enter here.

00:51.140 --> 00:58.220
We would go to slash joke and yeah you're usually not you know typing these you URLs manually you're

00:58.220 --> 00:59.400
clicking on links.

00:59.480 --> 01:03.950
So maybe there would be a link that takes us to slash joke just as there are links down here.

01:03.950 --> 01:06.700
This is an error page for cloud nine but there are links.

01:06.710 --> 01:08.500
Each one takes us to No.

01:08.540 --> 01:09.800
This one is.

01:09.960 --> 01:11.860
Io slash support.

01:11.900 --> 01:17.220
You can see that in the bottom left very tiny Here you can see slash support.

01:17.270 --> 01:23.180
So there is code that is listening taking this slash support and it's figuring out oh we need to show

01:23.180 --> 01:25.310
the support page that need help.

01:25.340 --> 01:31.160
All these buttons on support anyways so we're going to ask for Slash joke.

01:31.370 --> 01:37.180
Our express app is going to say Sure thing here you go and send back a really stupid joke.

01:37.280 --> 01:42.610
What do you call a dog that does magic tricks a lab or a cadabra door.

01:42.740 --> 01:48.260
I had to pick something that is politically neutral so I couldn't offend anybody.

01:48.260 --> 01:50.810
So we're just going broke in Labrador.

01:51.120 --> 01:53.600
So to implement that it's very similar to what we've done.

01:53.600 --> 02:00.350
We just basically duplicate the code we have except we change the route to the string of what we're

02:00.350 --> 02:01.440
trying to match.

02:01.580 --> 02:07.060
So basically in our app that J.S. file will have two questions which are pseudo code.

02:07.100 --> 02:09.920
The first one is did the client request a slash.

02:09.920 --> 02:11.180
That's what we already have.

02:11.570 --> 02:17.690
If they did respond with Welcome to the home page then we'll also have the client request slash joke

02:17.930 --> 02:20.390
and if they did respond with Knock knock.

02:20.390 --> 02:26.000
So only one of these is going to run every request because you can't send a request that is simultaneously

02:26.000 --> 02:28.040
to slash and slash joke.

02:28.130 --> 02:31.990
So we get one response depending on what we request.

02:32.000 --> 02:34.930
So without further ado this is all we have to do.

02:35.450 --> 02:36.050
I apologize.

02:36.050 --> 02:43.350
It's kind of long because that joke is long otherwise the code is pretty short don't get sloshed joke.

02:43.490 --> 02:45.950
Function request response.

02:45.950 --> 02:51.870
And then in here we just have a joke variable equal to our dumb joke and then we're doing a red dots

02:51.890 --> 02:53.780
and joke.

02:53.780 --> 02:59.830
So if we compare that over here to our existing right it's very very similar.

03:00.020 --> 03:04.360
So we'll get used to typing get.

03:04.520 --> 03:05.540
You know it's just terrible.

03:05.560 --> 03:06.770
But you do it a lot.

03:06.770 --> 03:09.640
Apt get and then we'll do joke.

03:10.220 --> 03:14.990
And that's just something I picked arbitrarily right to have a joke about when we're actually working

03:14.990 --> 03:15.890
with her.

03:16.250 --> 03:18.810
We'll talk about the route to need for join us.

03:18.890 --> 03:24.820
But for now we're working with Slash joke request and response.

03:24.950 --> 03:29.990
Once again we can call these whatever we want but wreck and red is pretty standard then in here.

03:29.990 --> 03:31.580
I'm just going to copy that joke.

03:31.610 --> 03:34.970
I don't want to make you watch me type that.

03:35.060 --> 03:35.430
All right.

03:35.430 --> 03:42.980
So we have that and what we can do is the same thing you did earlier concert out log requested the joke

03:43.590 --> 03:44.720
route.

03:45.290 --> 03:48.270
Quick quiz what will happen when I run node.

03:48.370 --> 03:52.020
Yes well when I just run it nothing will happen unless we had an error.

03:52.070 --> 03:57.830
But what will happen when I make a request to slash joke and the easiest way to do that is if I do preview

03:58.340 --> 04:05.300
preview running application and if I change this to slash joke here or I could go over here and do it

04:05.300 --> 04:11.070
in a separate window but if I do a joke and I hit Enter What do you think will happen.

04:14.390 --> 04:15.790
It just keeps spinning and spinning.

04:15.790 --> 04:19.690
We get requests to the joke route which is what we wanted.

04:19.920 --> 04:25.700
We constantly logged that means this route is being hit and we running this code.

04:26.050 --> 04:31.750
But now over here it's still spinning and spinning spinning and eventually it will stop and it will

04:31.750 --> 04:31.960
be.

04:31.990 --> 04:35.990
It will give us an error message saying cannot get slashed joke.

04:36.010 --> 04:40.810
Basically that's saying you know I tried we waited and waited waited but there was no response from

04:40.810 --> 04:41.550
the server.

04:41.800 --> 04:46.750
So the server received it and we did something but we never sent anything back because we're missing

04:46.810 --> 04:47.890
the rest sent.

04:48.100 --> 04:50.690
So very simple remedy get.

04:50.830 --> 04:52.810
We can have this come to the log if we want it.

04:52.990 --> 05:00.100
But it served its purpose now it's to arrest send and all we want to send is the joke.

05:00.280 --> 05:00.900
OK.

05:01.180 --> 05:02.920
So I'm going to stop this one.

05:03.130 --> 05:08.080
Well actually I'll just restart the server and that will kill it immediately.

05:08.080 --> 05:16.400
Now we'll do node after you again make this bigger I can get it go to slash joke and hit enter.

05:16.410 --> 05:19.490
Now I get this hilarious joke we've never heard before.

05:19.580 --> 05:22.560
What do you call a dog that does magic tricks.

05:22.560 --> 05:23.830
Elaborate cadaver dog.

05:24.170 --> 05:24.880
It's pretty good.

05:24.960 --> 05:25.710
Pretty good.

05:25.710 --> 05:27.750
Who ever came up with that one.

05:27.750 --> 05:28.770
And that's it.

05:28.770 --> 05:32.200
We have our two routes now so we could toggle between them.

05:32.490 --> 05:33.290
Well e-bay.

05:33.310 --> 05:35.130
I don't know where that came from.

05:35.130 --> 05:35.830
Here you go.

05:36.000 --> 05:43.470
So I've got the home route slash the route route or slash joke and that's it.

05:43.470 --> 05:47.150
So really the point here is just to show you you can have two routes.

05:47.310 --> 05:53.550
And usually applications have a ton of routes 10 20 depending on how complex the application is something

05:53.550 --> 05:58.010
like Cloud 9 are you to me might have 50 or 100 different things.

05:58.030 --> 06:03.120
Well hundreds extreme but there's a lot of different routes to wrap up.

06:03.120 --> 06:04.720
Let's add one last route.

06:04.800 --> 06:11.040
This one is a little different but it will do is return a random number randomly generated so it's not

06:11.040 --> 06:15.360
going to be hard coded in like you've reached the home page or what do you call it a dog that does magic

06:15.360 --> 06:15.650
tricks.

06:15.660 --> 06:17.810
Elaborate Labrador ha ha very funny joke.

06:17.940 --> 06:20.910
Rather than that it's going to be dynamically generated.

06:21.090 --> 06:23.040
But everything else is the same.

06:23.100 --> 06:28.950
So it's apt get slash and we're doing random name or number either one.

06:29.100 --> 06:33.550
I did know in the notes come a function request and response again.

06:33.660 --> 06:39.500
So whatever's in here will only be executed whenever someone requests slash a random number.

06:39.630 --> 06:44.270
So we'll do our Epcot's and excuse me redstart send.

06:44.640 --> 06:48.240
And let's just start just hard coding a number like 7.

06:48.930 --> 06:52.500
I'll do it as a string just to be safe if we restart the server.

06:52.500 --> 06:55.840
Now go over here.

06:55.830 --> 06:57.960
Joke still works.

06:58.170 --> 06:59.430
Home still works.

06:59.880 --> 07:05.160
But now we have random underscore Gnome and I get the number 7.

07:05.520 --> 07:08.520
So now all is left is to generate a random number.

07:08.760 --> 07:11.480
And the way we do it in javascript is kind of noxious.

07:11.560 --> 07:13.440
It's much easier in other languages.

07:13.440 --> 07:19.030
So there is a math dot random.

07:19.170 --> 07:24.360
And what this will do I'll actually just show you right now if I copy it over I need to stop the server

07:24.870 --> 07:30.180
I'm going to open just regular node which is not something I've shown you but node has just like my

07:30.180 --> 07:30.300
ass.

07:30.300 --> 07:36.900
Q Well has a CLIA where you can type things and get a response immediately can do one post one and get

07:36.900 --> 07:38.790
to know it has the same thing.

07:38.790 --> 07:43.540
If you just type node with a file name then it opens up Node shell.

07:43.800 --> 07:51.430
So if I do mapped out random and I don't capitalize that are now I do math at random.

07:51.460 --> 07:55.590
Notice that it gives you a random number between 0 and 1.

07:55.600 --> 07:58.650
It's really annoying that it does it this way in my opinion.

07:58.690 --> 08:06.400
So if we want to say something between 1 in 10 there's a couple of steps involved we need to generate

08:06.400 --> 08:09.640
or mapped out random we need to multiply it by 10.

08:09.640 --> 08:11.050
So we'll start with that.

08:11.460 --> 08:11.880
OK.

08:11.920 --> 08:16.220
Now we're getting one point seven seven point four eight point six.

08:16.390 --> 08:18.490
Then what we need to do is get rid of that decimal.

08:18.610 --> 08:23.480
So you can either round it's more standard is to do a mass of floor.

08:24.190 --> 08:31.320
So what that will do is just chop off the decimals so we end up with 8 9 8 2.

08:31.420 --> 08:34.660
But then we have a problem which is we never actually hit 10.

08:34.660 --> 08:40.050
We're doing a number between 0 and 9 which you have to trust me that's how it works.

08:40.060 --> 08:45.280
You can see we have zero here and it's because when we did our original math thought random This is

08:45.340 --> 08:49.610
a number between 0 and 1 non-HD including here not including one.

08:49.630 --> 08:52.090
So up up two point nine nine nine nine nine.

08:52.420 --> 08:55.000
So all we want to do is then shift it by 1.

08:55.150 --> 08:59.370
So it can have a lot of work to generate a random number between 1 and 10.

08:59.530 --> 09:03.460
But now we've got it just to prove OK there's a 10.

09:03.460 --> 09:04.440
We're in the clear.

09:04.690 --> 09:06.450
So now we'll just make that a variable.

09:06.440 --> 09:08.860
We'll just call it X maybe equals.

09:08.870 --> 09:14.680
Well maybe now is a better name make it a little clearer and then we'll just send it back we'll say

09:15.620 --> 09:21.510
or I think I said lucky number is Plus no.

09:21.870 --> 09:24.430
And that will just concatenated in there.

09:25.090 --> 09:25.810
Let's try it.

09:25.810 --> 09:29.230
So to get out of node we just do Control-C twice.

09:29.230 --> 09:32.230
Now we actually execute our app.

09:34.790 --> 09:41.780
Now if I go to a random number says Magic Numbers Three out of 10 notes 8 8 again 4 you get the picture.

09:42.200 --> 09:47.690
And of course we still have our home route and refreshing the adorable and hilarious Lauber cadaver

09:47.690 --> 09:48.560
dog joke.

09:48.850 --> 09:50.700
OK we're done here.

09:50.810 --> 09:54.230
Moving on in our next video to actually integrating my ask.

09:54.290 --> 09:56.410
Well with this web app big step.
