Okay.
Welcome back.
So in the last section, we saw the syntax to add a single route to an express app.
Our app has one URL that you can request to get a response.
Everything else, we get that error message that says cannot get and then whatever you tried to get,
whatever you tried to request.
So we'll try fixing that now.
We're going to add some more routes.
So the first one we'll do is going to be silly.
It's a joke.
Root When you go to slash joke, it just gives you a joke rather than just doing hello or hi or goodbye
or doing a joke.
So when we request from a computer or from a phone even to our URL, and then we have our express app
that we're requesting this time.
Our request is, hey, I would like the slash joke page.
So that's like this.
Rather than just hitting enter here, we would go to slash joke and yeah, you're usually not typing
these URLs manually, you're clicking on links.
So maybe there would be a link that takes us to slash joke just as there are links down here.
This is an error page for Cloud9, but there are links.
Each one takes us to this.
One is c 9.0 slash support.
If you can see that in the bottom left, very tiny here.
You can see slash support.
So there's code that is listening, that's taking this slash support and it's figuring out, oh, we
need to show the support page, the need help, all these buttons on support anyways.
So we're going to ask for slash joke.
Our Express app is going to say, sure thing, here you go and send back a really stupid joke.
What do you call a dog that does magic tricks?
A Libra cadaver door?
I had to pick something that is politically neutral.
It's not going to offend anybody.
So we're just going with the abracadabra door.
So to implement that, it's very similar to what we've done.
We just basically duplicate the code we have, except we change the root, the string of what we're
trying to match.
So basically in our app JS file will have two questions which are pseudo code.
The first one is did the client request slash?
That's what we already have.
If they did respond with Welcome to the home page, then we'll also have did the client request slash
joke and if they did respond with knock knock.
So only one of these is going to run every request because you can't send a request that is simultaneously
to slash and slash joke.
So we get one response depending on what we request.
So without further ado, this is all we have to do.
I apologize.
It's kind of long because the joke is long, otherwise the code is pretty short.
Get slash joke, function request response.
And then in here we just have a joke variable equal to our dumb joke and then we're doing a resend joke.
So if we compare that over here.
To our existing route is very, very similar.
So we'll get used to typing forget.
Yeah, that was just terrible.
But you do it a lot.
Target and then we'll do joke.
And that's just something I picked arbitrarily, right, to have a joke about.
When we're actually working with our app, we'll talk about the route we need for join us.
But for now we're working with joke, request and response.
Once again, we can call these whatever we want, but rec and red is pretty standard.
Then in here I'm just going to copy the joke.
I don't want to make you watch me type that.
All right.
So we have that.
And what we can do is the same thing you did earlier.
Console.log requested the joke root.
Quick quiz.
What will happen when I run Node app JS When I just run it, nothing will happen unless we had an error.
But what will happen when I make a request to slash joke?
And the easiest way to do that is if I do preview preview running application and if I change this to
slash joke here or I could go over here and do it in a separate window.
But if I do slash joke and I hit enter, what do you think will happen?
It just keeps spinning and spinning.
We get requests to the joke route, which is what we wanted.
We cancelled out logged.
That means this route is being hit and we're running this code.
But now over here, it's still spinning and spinning.
Spinning.
And eventually it will stop.
And it will.
It will give us an error message saying cannot get joke.
Basically that's saying, you know, I tried.
We waited and waited and waited.
But there was no response from the server.
So the server received it and we did something, but we never sent anything back because we're missing
resend.
So very simple remedy.
Get rid.
We can have this console.log if we want it, but it served its purpose.
Now let's do a red send and all we want to send is the joke.
So I'm going to stop this one.
Well, actually, I'll just restart the server and that will kill it immediately.
Now I'll do node JS again.
Make this bigger.
If I can get it, go to slash joke and hit enter.
And now I get this hilarious joke we've never heard before.
What do you call a dog that does magic tricks?
Elaborate cadaver door.
It's pretty good.
Pretty good.
Whoever came up with that one, and that's it.
We have our two routes now, so we could toggle between them.
What we eBay.
I don't know where that came from.
Here we go.
So I've got the home root slash, the root root or slash joke.
And that's it.
So really the point here is just to show you, you can have two routes and usually applications have
a ton of roots 1020 depending on how complex the application is, something like Cloud nine or Udemy
might have 50 or 100 different things.
Well, hundreds extreme, but there's a lot of different routes to wrap up.
Let's add one last route.
This one is a little different.
What it will do is return a random number randomly generated.
So it's not going to be hardcoded in like you've reached the home page or what do you call it, a dog
that does magic tricks.
Abracadabra.
Ha ha.
Very funny joke.
Rather than that, it's going to be dynamically generated, but everything else is the same, so it's
apt.
Get slash and we're doing random num or number either one i did num in the notes comma function request
in response again.
So whatever's in here will only be executed whenever someone requests slash random num.
So we'll do our app send or excuse me resend and let's just start by just hard coding a number like
seven.
I'll do it as a string just to be safe if we restart the server now go over here.
Joke still works, home still works.
But now we have random underscore num and I get the number seven.
So now all that's left is to generate a random number and the way we do it in JavaScript is kind of
obnoxious.
It's much easier in other languages.
So there is a math dot random and what this will do, I'll actually just show you right now, if I copy
it over, I'm going to stop the server and I'm going to open just regular node, which is not something
I've shown you, but Node has just like my SQL has a CLI where we can type things and get a response
immediately.
You can do one plus one and get to Node has the same thing.
If you just type Node without a file name, then it opens up a node shell.
So if I do math random and I don't capitalize that ah, now I do math at random.
Notice that it gives me a random number between the zero and one.
It's really annoying that it does it this way, in my opinion.
So if we want, let's say something between one and ten, there's a couple of steps involved.
We need to generate our math random.
We need to multiply it by ten.
So we'll start with that.
Okay.
So now we're getting 1.7, 7.4, 8.6.
Then what we need to do is get rid of that decimal so you can either round.
What's more standard is to do a math flaw.
So what that will do is just chop off the decimals.
So we end up with eight, nine, eight two.
But then we have a problem, which is we never actually hit ten.
We're doing a number between zero and nine, which those have to trust me.
That's how it works.
But you can see we have zero here and it's because when we did our original math random this is a number
between zero and one non including or not including one.
So up to a .99999.
So all we want to do is then shift it by one.
So it's kind of a lot of work to generate a random number between one and ten.
But now we've got it just to prove, okay, there's a ten, we're in the clear.
So now we'll just make that a variable.
We'll just call it X, maybe equals.
Well, maybe.
No, it's a better name.
Make it a little clearer and then we'll just send it back.
We'll say your and I think I said lucky number is plus num and that will just concatenate it in there.
Let's try it.
So to get out of Node, we just do control C twice now we actually execute our app is.
Now if I go to random num, it says my lucky number is three.
Now it's ten.
Now it's eight.
Eight again four.
You get the picture.
And of course, we still have our home root and our refreshingly adorable and hilarious.
Abracadabra, Dora.
Joke.
Okay, we're done here.
Moving on in our next video to actually integrating my SQL with this Web app.
Big step.