req and res objects in Express Js

You must have seen the req and res object passed to the callback functions in Express Js. You may have wondered, what are these parameters.

req object :

req object is passed to any route handler function or in middlewares. It is the object which contains the details sent by the client.

It contains the informations like the headers, url details including pathname, query, port, href, protocol , cookies, any type of form data in string or json format and so on.

res object :

res object is passed by the server to the client. It has various fields like headers, the status code, the page data which the user has asked for, details regarding http protocol etc.

next() :

Another parameter which is generally passed to a middleware function is “next”. next is a reference to the next middleware or route handler function in event loop. If we don’t call next at the end of middleware execution the request will be kept hanging and the user will just see the page loading.

Leave a Reply

Your email address will not be published. Required fields are marked *