How to handle request information in HTTP modules in Node.js

overview

In Node.js HTTP modules, you handle requests inside event listeners. A request object and a response object are passed as arguments to the event listener. You use these objects to retrieve data related to requests, generate responses, and so on.

Here, I will explain how to handle requests with Node.js using the following versions.

nodejs v19.7.0

In addition, all the code created this time is posted on GitHub.

How to get request information

Use the request object to get information about the request. A request object stores information about a request from a client. Used within HTTP module event listeners to access data such as request headers, parameters, and body.

The sample code below is an example for understanding the request object in Node.js. Receive a GET request, access request headers, query parameters, and return a response containing those values.

request.js

const http = require('http');
const url = require('url');

const server = http.createServer((req, res) => {
  // get request headers
  const headers = req.headers;

  // get query parameters
  const queryParam = url.parse(req.url, true).query;

  const response = {
    headers: headers,
    queryParam: queryParam,
  };

  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify(response));
});

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

Commentary

  • Import HTTP module and create server
const http = require('http');
const url = require('url');

const server = http.createServer((req, res) => {
  // code omitted
});

Import the HTTP and URL modules and create instances of them.

  • Defining route handlers
const server = http.createServer((req, res) => {
  // get request headers
  const headers = req.headers;

  // get query parameters
  const queryParam = url.parse(req.url, true).query;

  const response = {
    headers: headers,
    queryParam: queryParam,
  };

  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify(response));
});

Handles GET requests to the root (’/’). When a request comes in, get the request headers and query parameters and return an object containing them in JSON format as a response.

  • Start server
server.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Make the HTTP server listen on port 3000 and start the server. When the server starts, print the message “Server is running on port 3000” to the console.

test

curl http://localhost:3000

summary

Using the req object allows you to:

  • You can access request data such as request headers and query parameters.
  • Get data for different parts of the request, such as request path parameters and body data.

Using the req object, you can perform various operations on the request data. The req object has many properties and methods that allow for a high degree of customization and control.

Note that plain Node.js does not have the concept of validation or middleware functions, which limits some of the functionality available in Express.js. If you need more complex request processing or error handling, consider using Express.js or another framework.