InterviewSolution
| 1. |
What are the method responses? |
|
Answer» The method responses or the res object is the response sent by the server to the client. There are many types of responses which can be sent to the client and we will LOOK into them next. The most common method for res object is the .send(), through which we can send any DATA to the client. Below is a common example for the same. Here, if the user goes to the / route in their browser they will be shown the Hello Express text on the browser. We can also send the STATUS code with the .status() method. These HTTP status codes are very important and interpreted by the client browser as success or failures. In the below example, we have also chained the send method. There is a shorthand syntax also, which combines both status and send method CHAINING. The method is called .sendStatus() and it is used as below. We can also define headers in our responses to the client and for this we have the .append() method. Below is an example for the same. The .redirect() method is a very important one, as it can redirect to any route in the web-app. We have to just PASS the route as a parameter. We can also prompt the client to download a file with the .download() method. Below is an example for the same. |
|