- [Instructor] When creating routes with Express,
we will use various methods to handle the response
and provide feedback to the client.
Response methods are the methods on the response object
that are responsible for transmitting information
to the client, if we don't handle the responses,
or do so incorrectly, then the necessary information
will not make it from the server to the client.
In this video, we are going to discuss
some useful response methods
and how to use them in your projects.
The Express documentation has a full list
of response methods that you can incorporate
into your projects but, for the sake of this course,
we're only going to focus on a few of them
to help get you started.
Some of these should look familiar to you
as we have been using the JSON method
and the send method at various points
in our project thus far.
The JSON method simply sends the JSON response.
Right now, we're using this at the root
of our project to display our mock data.
The send method sends the HTTP response to the client.
We've been using this mainly to send strings
to the client, printing various messages in the browser.
The other two methods are new to us.
The download method is used to transfer
a file as an attachment, depending on your browser,
you may be prompted to download a file,
or it will just automatically download
the file to your computer.
The redirect method is used to redirect
the client to a different URL.
This could be local to the application,
or a completely different website.
Let's head to our editor and create some new routes using
the response methods we just learned about.
For ease in creating our redirect method route
let's just go ahead and copy our original get.
And we'll paste this in right below
the download method that we created just a second ago.
And I'll add a note that this is our redirect method.
Our redirect method, we're going to have
that at the path redirect so we can save that.
And then, we can actually remove this response .json.
So with the redirect method it will be applied
to the response object,
and the redirect method, as discussed previously,
it can redirect you to an external site,
or it can redirect you within your application.
But, for this particular example,
we're going to use an external site.
So, within the parentheses, the argument
that we're going to pass here to this redirect method
is in quotation marks, we're going to go to the LinkedIn URL.
So that is http://www.linkedin.com.
So when we go to localhost:3000/redirect
then we're going to be automatically redirected
to the LinkedIn page.
So let's test us in our browser
and make sure it's working as expected.
All right so if we change this to redirect and hit Enter,
you'll see that you are automatically redirected
to the LinkedIn home page, great work.
Remember, this is not an exhaustive list
of response methods that are available.
You can check out the Express documentation
for additional methods to help you build future projects.