An request express object comes into play when using expressjs to help work with incoming http requests. The express request object that is one of four potential arguments that can be used when making an middleware function that that can be used when making application or router level middleware typically with a method like app.get, or app.use. The request object contains all kinds of useful properties and methods when it comes to working with incoming http requests. There are also of course response objects as well but, in this post I will be writing about some of the must know features of request objects when working with express.js.
When making any kind of request from a client to an express.js app, the incoming request object is the first argument in the function that you give an express.js app method like app.get.
The request object contains important information about the request, such as headers, a data payload if it is a post request, and so on. This object, along with the response object is a major part of what express.js is all about. A request is received, something is done with that request, and an appropriate response is given back to the client.
For starters one might make a simple script that just responds to a get request to the root path.
In this example I just sent back the request object. I had to use the node.js built in util.inspect to make it into a string, because the request object contains circular references.
The req.body property can be used to get a data payload that was sent from the client system when working with post requests. In order to use this a body parsing middle ware will need to be used, luckily one comes with express itself.
A full working demo of this is a little involved as a front end is needed on top of back end code that uses the body-parser module. I have written a post where I get into this in detail.
In the request object there are of course the htp request headers that can be found at req.headers, and the req.get method can be used to get a header if you know the name of it.
I have a full demo in a post that I have wrote on http request headers.
This post does not do all of what there is to know about in request objects justice, and it may never will. When it comes to updating this content in the future this post will likely serve as a kind of index to additional posts on things like the body-parser module that is used to populate the body property of a request object. As my content on express.js grows so should the content of this post.