Open In App

Node.js http.ClientRequest.host API

Last Updated : 08 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The http.ClientRequest.host is an inbuilt API : Application Programming Interface of class ClientRequest within http module which is used to get the object of client request host.

Syntax:

const request.host

Parameters: This API does not accept any argument as parameter.

Return Value : This method returns the object of client request host.

Example:

JavaScript
// Node.js program to demonstrate the  
// request.host APi

// Importing http module
const http = require('http');

// Create an HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('okay');
});

// Now that server is running
server.listen(3000, '127.0.0.1', () => {

  // make a request
  const options = {
    port: 3000,
    host: '127.0.0.1',
    headers: {
      'Connection': 'Upgrade',
      'Upgrade': 'websocket'
    }
  };

  // getting client request
  const req = http.request(options);

  req.host='127.0.0.1'

  // getting host
  // by using host api
  const v = req.host;

  // display the result
  console.log("host :- " + v)

  process.exit(0)
});

Output:

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_host


Next Article

Similar Reads