Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node
connection
property
request
communitycreator

What is the Node request property "connection"?

Ayush Trivedi

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In this shot, we will learn about the request property connection, which is a property similar to the socket property.

The request.connection property returns all the details of the connection between the server and the client, and the object has details like localAddress, socket.writable, socket.readable, etc.

Code

Let’s try to use request.connection and get all the connection details created.

const http = require('http')
const port = 8080
const server = http.createServer((req,res)=>{
res.statusCode = 400;
res.end();
});
const listener= server.listen(port, () => {
console.log(`Server running at port ${port}`)
var request = http.request({
port: 8080,
host: '127.0.0.1',
});
request.end();
request.once('response', (res) => {
console.log(res.statusCode);
console.log(request.connection);
})
})
setTimeout(()=>{
server.close()
},2000)

Explanation

  • In line 1, we imported the HTTP module.

  • In line 3, we defined the port number of the server.

  • In line 5, we used http.createServer() to create a server and listen to it on port 8080.

  • In line 6, just for learning purposes, we defined res.statusCode to 400, which will be returned when the request is made.

  • In line 10, we listed the server using server.listen(). When the server is running, we request the callback function.

  • In line 13, we made a HTTP request using http.request(). It is a GET request by default, with parameters defined in the object.

  • In line 18, request.end() is important, as it finishes the request. If we don’t use end(), the request will be made, but the server will wait for the incoming data until end() is called.

  • In line 20, we listened to the response event on the request we made. In this event, we will print the connection details using the request.connection property, and it will return all the details of the connection.

  • In line 26, we used setTimeout() because the Educative feature of a running server will not stop until we stop it by calling server.close(). It will still give you the desired result, but with an execution timeout error. You don’t need to write this on your machine.

Output

When we listen to the response event and print the connection details, we get the connection details on the console, and it is a JSON object with lots of connection detail.

RELATED TAGS

node
connection
property
request
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring