Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com> Co-authored-by: Ilenia M <nethleen@gmail.com>
4.0 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69a9183588765b5a18d20954 | What Are the Different Response Methods in Express? | 19 | what-are-the-different-response-methods-in-express |
--description--
In Express, the res (response) object is used to send responses back to the client after processing incoming requests. Express provides several built-in methods for sending different types of responses. For example, you can send plain text, JSON data, redirect a user to another URL, or render a template.
One of the most commonly used response methods is res.send():
app.get("/text", (req, res) => {
res.send("Hello, World!");
});
The res.send() method sends a response back to the client. It can send plain text, HTML, JSON, or even binary data. In this example, when a client visits /text, the server responds with the text Hello, World!.
Another commonly used response method is res.json().
This method sends a JSON response to the client. It’s especially useful when building APIs, where you often need to return structured data such as objects or arrays. Express automatically converts a JavaScript object into a JSON-formatted string:
app.get("/user", (req, res) => {
res.json({ name: "John Doe", age: 30 });
});
If a client sends a request to /user, the server responds with the following:
{
"name": "John Doe",
"age": 30
}
When you need to control the HTTP status code of a response, you can use res.status(). This method sets the status code and is typically chained with another response method like res.send() or res.json():
app.get("/notfound", (req, res) => {
res.status(404).send("Not Found");
});
Here, the server sets the status code to 404 and sends the message Not Found back to the client.
If you want to send the user to a different URL, you can use res.redirect():
app.get("/oldpage", (req, res) => {
res.redirect("/newpage");
});
When a client visits /oldpage, they are automatically redirected to /newpage.
Finally, res.render() is used when working with template engines such as Pug, EJS, or Handlebars. It renders a view template and sends the resulting HTML to the client:
app.get("/profile", (req, res) => {
res.render("profile", { username: "john_doe" });
});
In this example, the server renders a profile view and passes a username variable to it, which can then be displayed in the HTML.
These response methods allow you to return different types of data based on the nature of the request. Whether you're sending text, JSON data, or redirecting the client, Express has a flexible set of tools to handle it.
--questions--
--text--
What does the res.json() method do?
--answers--
Sends a JSON response to the client.
Sends plain text to the client.
--feedback--
Think about the format of the data you're sending back.
Sets the status code of the response.
--feedback--
Think about the format of the data you're sending back.
Redirects the client to another URL.
--feedback--
Think about the format of the data you're sending back.
--video-solution--
1
--text--
How would you send a 404 status code with a custom message?
--answers--
res.send(404, 'Not Found')
--feedback--
What method do you use to set the status code?
res.status(404).send('Not Found')
res.redirect('/404')
--feedback--
What method do you use to set the status code?
res.status(500).send('Error')
--feedback--
What method do you use to set the status code?
--video-solution--
2
--text--
What does the res.redirect() method do?
--answers--
Sends a file to the client.
--feedback--
Think about what happens when a URL redirects to another URL.
Redirects the client to another URL.
Sends JSON data to the client.
--feedback--
Think about what happens when a URL redirects to another URL.
Sets the response status code.
--feedback--
Think about what happens when a URL redirects to another URL.
--video-solution--
2