0
0
Node.jsframework~15 mins

Request object properties in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Explore Request Object Properties in Node.js
📖 Scenario: You are building a simple Node.js server that listens to HTTP requests. You want to learn how to access different properties of the request object to understand what the client is sending.
🎯 Goal: Create a Node.js HTTP server that reads and logs specific properties from the request object: the HTTP method, the URL, and the headers.
📋 What You'll Learn
Create a basic HTTP server using Node.js http module
Access the method property of the request object
Access the url property of the request object
Access the headers property of the request object
💡 Why This Matters
🌍 Real World
Web servers need to read request properties to understand what the client wants and respond correctly.
💼 Career
Knowing how to access request properties is essential for backend developers working with Node.js to build APIs and web applications.
Progress0 / 4 steps
1
Set up a basic HTTP server
Create a Node.js HTTP server using http.createServer and assign it to a variable called server. The server should have a callback function with parameters req and res. Do not add any logic inside the callback yet.
Node.js
Need a hint?

Use http.createServer with a function that takes req and res as parameters.

2
Add a port number variable
Create a constant variable called port and set it to 3000. This will be the port your server listens on.
Node.js
Need a hint?

Use const port = 3000; to define the port number.

3
Access and store request properties
Inside the server callback, create three constants: method assigned to req.method, url assigned to req.url, and headers assigned to req.headers.
Node.js
Need a hint?

Use const method = req.method; and similarly for url and headers.

4
Start the server listening on the port
Add a line to make the server listen on the port variable using server.listen(port).
Node.js
Need a hint?

Use server.listen(port); to start the server.