Okay well come back.
So in the last section we saw the syntax to add a single route to an express app our app has one.
You are all that you can request to get a response.
Everything else we get that error message that says cannot get.
And then what if 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 will do.
It's going to be silly it's a joke route.
When you go to slash joke it just gives you a joke rather than just doing hello or high or goodbye or
doing a joke.
So when we request from a computer or from a phone even to or you are.
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 you know typing these you 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 cloud nine but there are links.
Each one takes us to No.
This one is.
Io slash support.
You can see that in the bottom left very tiny Here you can see slash support.
So there is code that is listening taking this slash support and it's figuring out oh we need to show
the support page that 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 lab or a cadabra door.
I had to pick something that is politically neutral so I couldn't offend anybody.
So we're just going broke in Labrador.
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 route to the string of what we're
trying to match.
So basically in our app that J.S. file will have two questions which are pseudo code.
The first one is did the client request a slash.
That's what we already have.
If they did respond with Welcome to the home page then we'll also have 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 that joke is long otherwise the code is pretty short don't get sloshed 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 red dots
and joke.
So if we compare that over here to our existing right it's very very similar.
So we'll get used to typing get.
You know it's just terrible.
But you do it a lot.
Apt get 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 her.
We'll talk about the route to need for join us.
But for now we're working with Slash joke request and response.
Once again we can call these whatever we want but wreck and red is pretty standard then in here.
I'm just going to copy that 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 concert out log requested the joke
route.
Quick quiz what will happen when I run node.
Yes well 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 a 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 constantly logged that means this route is being hit and we running this code.
But now over here it's still spinning and spinning spinning and eventually it will stop and it will
be.
It will give us an error message saying cannot get slashed joke.
Basically that's saying you know I tried we waited and waited 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
the rest sent.
So very simple remedy get.
We can have this come to the log if we want it.
But it served its purpose now it's to arrest send and all we want to send is the joke.
OK.
So I'm going to stop this one.
Well actually I'll just restart the server and that will kill it immediately.
Now we'll do node after you again make this bigger I can get it go to slash joke and hit enter.
Now I get this hilarious joke we've never heard before.
What do you call a dog that does magic tricks.
Elaborate cadaver dog.
It's pretty good.
Pretty good.
Who ever came up with that one.
And that's it.
We have our two routes now so we could toggle between them.
Well e-bay.
I don't know where that came from.
Here you go.
So I've got the home route slash the route route 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 routes 10 20 depending on how complex the application is something
like Cloud 9 are you to me 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 but it will do is return a random number randomly generated so it's not
going to be hard coded in like you've reached the home page or what do you call it a dog that does magic
tricks.
Elaborate Labrador 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 name or number either one.
I did know in the notes come a function request and response again.
So whatever's in here will only be executed whenever someone requests slash a random number.
So we'll do our Epcot's and excuse me redstart send.
And let's just start just hard coding a number like 7.
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 Gnome and I get the number 7.
So now all is left is to generate a random number.
And the way we do it in javascript is kind of noxious.
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 need to stop the server
I'm going to open just regular node which is not something I've shown you but node has just like my
ass.
Q Well has a CLIA where you can type things and get a response immediately can do one post one and get
to know it has the same thing.
If you just type node with a file name then it opens up Node shell.
So if I do mapped out random and I don't capitalize that are now I do math at random.
Notice that it gives you a random number between 0 and 1.
It's really annoying that it does it this way in my opinion.
So if we want to say something between 1 in 10 there's a couple of steps involved we need to generate
or mapped out random we need to multiply it by 10.
So we'll start with that.
OK.
Now we're getting one point seven seven point four eight point six.
Then what we need to do is get rid of that decimal.
So you can either round it's more standard is to do a mass of floor.
So what that will do is just chop off the decimals so we end up with 8 9 8 2.
But then we have a problem which is we never actually hit 10.
We're doing a number between 0 and 9 which you have to trust me that's how it works.
You can see we have zero here and it's because when we did our original math thought random This is
a number between 0 and 1 non-HD including here not including one.
So up up two point nine nine nine nine nine.
So all we want to do is then shift it by 1.
So it can have a lot of work to generate a random number between 1 and 10.
But now we've got it just to prove OK there's a 10.
We're in the clear.
So now we'll just make that a variable.
We'll just call it X maybe equals.
Well maybe now is a better name make it a little clearer and then we'll just send it back we'll say
or I think I said lucky number is Plus no.
And that will just concatenated in there.
Let's try it.
So to get out of node we just do Control-C twice.
Now we actually execute our app.
Now if I go to a random number says Magic Numbers Three out of 10 notes 8 8 again 4 you get the picture.
And of course we still have our home route and refreshing the adorable and hilarious Lauber cadaver
dog joke.
OK we're done here.
Moving on in our next video to actually integrating my ask.
Well with this web app big step.