Express end response method
The express end response method is one of several ways to go about ending an incoming http request from a client system. The express end method is used for situations in which the request is to just simply be put to an end without sending any data to the client, or preforming any kinds of other action such as redirection to another path.
It is true that the method can be used to send data in the form of a string or buffer to the client, but another response method should be used such as res.send, or res.json that are also at hand in a response object.
1 - Express end response method basic example
For a basic example of the express end response method here is an example that uses the app.all method to just simply end any request with the end method. When using the end method there is no need to pass any argument to it, data can be passed in the form of a string or buffer, but if the express end method is being used that way it might be better to use the send response method rather than the end method.
|
|
The end method is great for just simply ending a response and not sending any data. A response must always be made to a client one way or another, and some times it is necessary to just end the request with the current status code.
2 - The Express end method does not set http status
The use of the express end method does not do anything to change the http status code of the response. When using the end method to end a request the statusCode property can be used to find out the current http status code, and the status response object method can be used as a way to set the desired http status code. The express end response method can then be used to end the request with the set status code.
|
|
3 - Using Express end to send data to the client
Although it might not be a good idea to use the end response method to send data to the client, it can be used to send a string or a buffer. However the send response method should be used to do this. In addition using the end response method to send something like an object will result in an error, use the json response method to do this to send an object as JSON.
|
|
4 - Conclusion
So then the express end response method is one of several ways to end an http request from a client that is a good choice when no data needs to be sent to the client. There are a number of other options when it comes to ending a response though some of which might provide a wider range of flexibility. There is the res.send, res.sendFile, and res.redirect methods that come to mind when it comes to the various other options when working with express by itself. There is also using a layout engine of one kind or another and using the res.render method as a way to end a request.
For more expressjs related content on this site be sure to check out the main post on expressjs that will branch off into all kinds of other topics.