OK let's get started on the solution to this exercise and we'll begin by creating a new folder that
I'll just call solution.
So make your solution and CD into solution.
And then we need to create our package thought Jason.
So I'll do NPM in it.
That's usually the very first thing I do when I'm starting a new Express app before I make any files
.
I just run NPM in it and I'll just go with solutionis the name 1.0 point no description will be solution
to first express exercise.
Entry point will make app J.S. just a matter of preference test command and enter get repository hit
enter keywords hit enter author cold steel license that's fine.
Is this OK.
Yes.
OK so that gives us the package that Jason can see that here.
Now I'm going to make our act up.
Yes that's where I will write all the routes and the code
and then we need to install EXPRESS So NPM install Express and the important part is that I want you
to add as a dependency in the package stuff Jason.
And technically you could do that manually by going in and typing it in there but the idea is to use
dash dash save which will look for the package that Jason and automatically added in there for us as
you can see here.
Close out middies.
Here we go.
Now has dependencies and express listed under that.
OK so let's begin by setting up our basic express app.
So I'm going to open this app Julius and I'm going to require Express and this just takes the contents
of the Express module the express package and then we want to execute it and save it to a variable which
is commonly called app but it can be called anything just like that.
Now we have the app variable that will be used to set up our routes and let's go ahead and do the first
one which is the root.
So apt get slash and the message that we're looking for is either.
Welcome to my assignment so I'll just copy that over apt get and then we need our function with the
request and the response object.
And in here they set into start and we're going to do a rez send and just send that whole string and
save.
And let's go ahead and start the server.
So node Abdulle Yes and you'll notice that nothing happens.
And that's because we're missing the code at the bottom.
That actually tells express it tells the app to listen on a particular port.
So we need to add in app to listen and then we need to provide the port which is a process that EMV
ports and same thing for the IP.
So process NVI IP.
And I'd like to add in a little message afterwards so it accepts an optional callback where we can do
a console log.
No serving your app and let's save that and if we now start the server we get a little message and if
we go to the you are just the root and I hit enter.
I get either.
Welcome to my assignment which is what we expect.
So that's 1 out of 3 routes down.
Let's get started on this speak slash animal route and the main goal here is to have you use a route
parameter instead of the right definition.
So have another abduct get before a share.
The right way.
I'll show you what you weren't supposed to do which is slash speak slash pig and make that its own route
and then another one for kow that it's not very dry code especially if we had five different animals
.
That's not what you're expected to do.
I was looking for this slash speak slash colon animal and this can be named anything but I think animal
makes the most sense.
Maybe you could have called it creature as well but we could also just call it X which is definitely
not a great variable name it doesn't mean much but it will work the exact same way as animal.
And then let's add this in with our request in our response and here we know we're going to send something
back at the end so we can add that in.
And why don't we start by just saying this is the speak route.
Just to make sure our route parameters are set up correctly and if we restart the server go back and
refresh and to trigger it we need to go to speak slash something slash anything and we get this is the
speaker.
OK so now let's focus on the logic here.
The first thing we want to do is just extract the animal.
So I'm going to make a variable just call it animal.
And remember it's in the Paramo so request up harams dot animal is how we access that.
Then the next thing we need to do is have a variable that will store the sound that the animal makes
.
So something like var sound and it will just be an empty string to start and then we'll basically check
if animal is equal to pig.
Then will say sound should be oink and did the same thing.
LCF animal equals cow.
And we can keep doing this.
Sound equals Moo.
And let's just leave it at that for now and add in our semi-colons and then redstart send and we'll
just send the plus animal plus another space says space plus sound.
And this only has two animals for now.
Because as you can see it's a little bit of work to get another animal in there.
It's an entire other state statement and I'll show you a shorter way of doing this in just a second
but let's make sure this works.
So if animals pig sound as if it's cow sounds moo and then read out and the animal says Plus the sound
.
Let's restart.
See if we have any syntax errors to start.
Looks good.
Let's refresh.
This is speak slash pig.
And yet the pig says oink and let's do the same thing for the cow.
The cow says moo.
Excellent.
Now let's go back and talk about the easier way that I mentioned where we don't have to have a separate
if statement for every single animal.
So rather than doing that I'm going to delete it all and I'm going to define a dictionary of animal
sound pairings which will just be a plain javascript object so I'll call it far sounds and inside we'll
have an animal like pig which will correspond to a sound point.
And then we'll have another one for cow which corresponds to mu.
And another one for a dog which will correspond to Wolf Wolf.
And then we'll add in cat as well which says I hate you human.
And the last animal will be goldfish and the goldfish says dot dot dot.
So now that we have this object sound that matches an animal with a sound although we need to do is
take the animal that the user is looking for.
So speak slash dog for instance and we take dog and plug it into sounds and we'll get Wolf Wolf back
out.
So we'll keep animal how it is we need this variable but sound is now going to equal sounds of animal
just like that.
And we should be good to go now.
Let's save restart the server refresh.
And it says the exact same thing.
Let's try goldfish.
The Goldfish says dot dot dot and cat the cat says I hate you him and there are two minor changes that
I'd like to make.
The first one is that I want quotes around the animal's sound.
So it says the cat says and then quote I hate you human.
And there's a few ways of doing that.
The simplest one is just to use single quotes around sound.
So we'll do something like this.
Or we add a single quote and then at the end we do plus a new string and all that goes inside that string
is a single quote.
Quotations can be such a pain sometimes in javascript but this isn't too bad.
We have single quotes instead of double quotes or for using single quotes in the double quotes inside
.
And now let's restart again
refresh and we get the cat says I hate you human.
End quote.
However if I go to cat with a capital C we have a problem.
Same thing if a good dog with a capital C.
And that's because inside of our sounds object they're are not upper case.
The key is here pig with a lowercase P dog with a lower case D and rather than duplicating it we could
do dog corresponds to Wolf Wolf.
That's really not dry.
What we could do instead is just down case the animal as it comes in.
So a request up for an animal got to lower case just like that.
And now we restart one more time refresh and now uppercase D for dog and lower case work just the same
.
There we go.
So that's it for this route.
The next route is the repeat route.
So we're going to have an apt get and we know we're going to have Slash repeats and then we need two
variables or two parameters in the route.
And the first one is the message.
So we'll just call it message to be repeated.
And the second one is the number of times.
And does call that times
and we'll add in our callback at the request and response that code that will be run when this route
is hit.
And inside of here why don't we start just capturing these two variables.
So var message equals request top harams message and var times equals requests.
Harams times and floods do a response send.
And we'll just do message plus message just to see what it is.
And we'll also do plus times is class times.
So just to make sure that we're getting message and times correctly restart the server again go back
refresh in this time go to repeat slash and we'll do hello slash one and we get messages Hello times
is one and if we change that.
It works just fine.
OK so now let's go back and work on actually getting this to work where we repeat message times number
of times and the first thing we want to do is make sure that times is a number.
Because right now it's a string.
So we'll do number requested perhaps sometimes just like that and then we need to do a loop.
So we're going to loop through.
So I'll just start with the for loop for var I equal.
Is less than times.
Plus plus just like that.
And then inside of here what we need to do is add a message to a variable.
So every time through the loop we'll add in message and we'll be making this giant Stringer potentially
a giant string that we're then sending at the end.
We can only do redstart send one time when you make a request to a Web site you get one response back
.
You don't get multiple responses and redstart send is sending the response so we can't just do rest
out send in the loop because it will only happen the first time.
And then we're done.
So we need to do a restart send.
And let's just do restart and result which is a variable that doesn't exist yet that we need to define
and we'll start with var results equals just empty string.
And then inside the loop each time we'll do result plus equals message just like that.
And this will then send back the result string that we've constructed that we've built up by going through
this loop.
So restart the server again refresh and just do something a little more manageable like hi slash 10
.
And it looks like we get 10 highest let's do three get three guys there.
But I do want to have spaces between them and to do that.
It's a simple change we'll just add a space.
We'll just add a space after we add in message and again restart refresh.
And now we get high space high space high.
And let's do a low slash 1000.
There we go.
Looks good to me.
The very last thing is getting the catchall route which is supposed to print sorry page not found.
What are you doing with your life to make this work.
We need to use the asterisk.
So we'll go back and just at the bottom of the file apt get star and then our function request and response
.
And then in here we'll do a redstart send and paste that string in and save the very last time restart
the server.
Let's go back refresh and try going to a route that doesn't exist like this.
Forget sorry.
Page Not Found.
What are you doing with your life.
All right so that's it for this exercise.
Coming up next we're going to keep working with Express.
We're going to talk about how we can work with HDMI all how we can send back an entire file of HDMI
now.
We're going to talk about templates and something called the Ejay Yes stands for embedded Javascript
.
And we're also going to talk about post requests