Express response objects
An express response object is one of four possible arguments that is passed to an express middleware function. Expressjs has to do with the use of middleware that does something with incoming http requests. So request objects have to do with the incoming http request from a client system, and response objects have to do with the response to that system. The other two arguments in an middleware method have to do with error handling, and passing along control to another middleware method. However in this post I will be focusing on just response objects today.
1 - Express Response and what to know before continuing
2 - Express response methods
The response methods in a express response object can be used to end an htp request and respond to that request for some kind of data or at least a status of some kind to let the client know what might have went wrong. In this section I will be going over the options when it comes to the response methods in an express response object. In addition I will link to additional posts for each that will go into these methods in further detail as there is much to cover with them.
2.1 - Express res.send response method
One of the most simple and easy to use response methods is res.send method. This comes in handy if I just want to send a string as a response when it comes to simple projects and demos. It might not be the best option when it comes to a more advanced project though, for that I often get into using a template system and the use of express.static.
If I where to pass an object rather than a string the res.send methods will send json, a number will be recognized as status code but it is not a good idea to use res.send to do so for these tasks. There is of course res.json, and res.sendStatus.
2.2 - Express res.render response method
The render express response object method is what can be used in conjunction with template files and a view engine to render html server side. This will require having an express view folder at some location in the project folder, or somewhere in the file system of a deployment that will contain template files. In this basic example I am using ejs as a template system, but there are many other options with this as well such as pug.
In the above example I am passing an empty object as the second argument after the name of the template I wan tot use in the view folder. This empty object can contain local variables for the template though that can be used in the rendering process.
2.3 - The res.redirect response method
Another way to end a request is to redirect the request to another path, this can be done with the express redirect response method.
3 - The Express response object response headers
So a very important aspect of express response objects is response headers. Headers can be set using methods like res.set, and res.type. I will not be getting into http response headers in depth here, that will be a whole other post naturally. However I will cover some basics about setting response headers in express in this section.
3.1 - The response type method
So the response type method can be used to set just the Content-Type header of a response object. The nice thing about this method is that I do not have to know the mime types, I can just use file extension names or some more common name for a file type. For example I can use text to set the text/plain mime type.
This might work okay for the most part, but it does not give the higher degree of control that the response set method will give.
3.2 - The response set method
The res.set method of an express response object can be used to set response headers for an http request response. Response headers can be set one at a time, or an object of key value pairs can be given to it as well.
I generally like to use the response set method over the response type method for setting the Content-Type header of a response. For the most part the mime types that are set by the response type method will work just fine, but setting Content-Type this way gives me a better degree of control over what is being set. It just makes things far more clear.