What are res and req objects in Node.js Express framework

res and req objects in Node.js Express framework

Introduction

In this article, we’ll learn request and response objects in node and understand How to use them. What information and capabilities do they provide. The easiest way to explore them is to log them.

For example, let’s got to console. log the req object here.

const http=require('http')

const requestListener = (req, res) => {
  console.log(req);
  res.write("Hello World\n");
  res.end();
};

const server=http.createServer();
server.on('request',requestListener);

server.listen(4253, () => {
   console.log('Server is running..');
});

Now to get this line to execute, we don’t need to restart Node, thanks to Nodemon, but we need to go make an HTTP request. because this is the function that gets executed per HTTP request.

Nodemon http requests

The request object has a lot of properties and the printing here is also printing everything nested under the main properties of the request object, which is a bit hard to read. To get a smaller output about this request object, use dir instead of a log, and pass in, as a second argument to it,

const http=require('http')

const requestListener = (req, res) => {
  console.dir(req, { depth:0 });
  res.write("Hello World\n");
  res.end();
};

const server=http.createServer();
server.on('request',requestListener);

server.listen(4253, () => {
   console.log('Server is running..');
});

The object with a property depth equal to 0. These means do not print any nested objects. Only print the first level of properties for this req object. Node has restarted. Refresh to see the new output, and now, we see only the first level of properties and any nested objects will not be printed here.

req and res nested objects in node

The request object is of type IncomingMessage. It’s the class that was used to internally instantiate a request object.

So, that little request object belongs to the IncomingMessage class. and the another ClientRequest class is used when you want to use the HTTP library as an agent to fetch information from an HTTP server, rather than as a server, which is what our example is doing. So remember, the request object within an HTTP server listener function is of type IncomingMessage.

We can use this response object to send a few things over to the requester. It can control things like the status code and the status message, the headers of the response, and any data we’d like to include in the response body, which is what our example is doing here.

const http=require('http')

const requestListener = (req, res) => {
  console.dir(res, { depth:0 });
  res.write("Hello World\n");
  res.end();
};

const server=http.createServer();
server.on('request',requestListener);

server.listen(4253, () => {
   console.log('Server is running..');
});

Both the request and response object are streams here.

request and response object serverresponse

The request object is a readable stream, while the response object is a writeable one. Because streams are all event emitters, we can subscribe to events emitted by these objects too.

How to Monitor Files for Changes

Node.js, in development, is a bit different than many other runtimes and frameworks. It does not auto-reload any changes. You’re going to have to remember to restart it, but let me show you a better option. The popular solution to this problem in Node is to monitor the operating system files for saving the event, and try to auto restart Node when you get these events.

There are many npm packages that are designed for this exact purpose. like nodemon.

$ npm i -g nodemon

you run your code with Nodemon instead of node. The Nodemon command is a wrapper around the node command, so the Nodemon command we typed will run our server as if we’re running it with the node command, but now it will monitor the files for any save events and reload itself when files are saved.

So, when we change this to “Node” and just hit the save button, Nodemon automatically restarted itself, and you’ll be able to see the new changes in effect. but you don’t need this Nodemon package in production. This is just a development convenience.

Leave a Reply

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