Node.js Tutorial: Handling Requests (req object) 🎯

beginner
13 min

Node.js Tutorial: Handling Requests (req object) 🎯

Welcome back to CodeYourCraft! Today, we're diving into a fascinating topic: Handling Requests (req object) in Node.js. This is a crucial concept for building web applications, and we'll cover it step-by-step, ensuring you're well-prepared to tackle real-world projects. 📝

What are Requests in Node.js? 📝

Requests, in the context of Node.js, refer to data sent to a server from a client (like a browser). When you access a web page, your browser sends a request to the server to retrieve the necessary data to display the page. In Node.js, we handle these requests using the req object.

Understanding the req object 💡

The req object is an instance of the http.IncomingMessage class and is automatically passed to the callback function in a http.createServer() method. It contains various properties and methods that help us understand and respond to the client's request.

Essential Properties of the req object 📝

  1. method: The HTTP method used in the request (e.g., GET, POST, DELETE, etc.)
  2. url: The URL the client is requesting.
  3. headers: An object containing the headers sent by the client.

Accessing Client Data 💡

We can access the client data sent with the request through the req.body, req.params, and req.query properties. Let's explore each one:

req.body

The req.body contains the body of the request. In a POST request, this is where we find the data sent by the client.

javascript
app.post('/example', (req, res) => { console.log(req.body); // ... });

req.params

The req.params object contains any dynamic parameters from the URL. In the following example, :id is a dynamic parameter:

javascript
app.get('/example/:id', (req, res) => { const id = req.params.id; // ... });

req.query

The req.query object contains any query parameters passed in the URL, after the '?' symbol. These parameters are usually used to filter or sort data.

javascript
app.get('/example', (req, res) => { const sortBy = req.query.sortBy; // ... });

Responding to Requests 💡

To respond to a request, we use the res object, which is also passed to the callback function in the http.createServer() method. The res object has various properties and methods that allow us to send data back to the client.

Essential Properties of the res object 📝

  1. statusCode: The HTTP status code to send as a response.
  2. json(): A method to send JSON data as a response.
  3. send(): A method to send plain text as a response.

Sending a Response 💡

Here's an example of sending a response using the res object:

javascript
app.get('/example', (req, res) => { res.statusCode = 200; res.send('Hello, World!'); });

Quiz Time! 🎯

Quick Quiz
Question 1 of 1

Which object in Node.js handles incoming HTTP requests?

Quick Quiz
Question 1 of 1

How can we access the body of a POST request in Node.js?

Keep learning, and we'll dive deeper into handling requests in Node.js in our next lesson! 🚀

Remember, practice makes perfect! Try implementing these concepts in your own projects and experiment with different types of requests and responses. Happy coding! 💡