Skip to content
Related Articles

Related Articles

Improve Article

Express.js req.subdomains Property

  • Last Updated : 07 Jul, 2020

The req.subdomains property contains an array of subdomains in the domain name of the request. The application property subdomain offset, which defaults to 2 which determines the beginning of the subdomain segments.

Syntax: 

req.subdomains

Parameter: No parameters.
Returns: Array
Installation of express module: 
 

  1. You can visit the link to Install express module. You can install this package by using this command. 
     
npm install express

  1. After installing the express module, you can check your express version in command prompt using the command. 
     
npm version express

  1. After that, you can just create a folder and add a file for example, index.js. To run this file you need to run the following command. 
     
node index.js

Example 1: 
Filename: index.js 
 

javascript






var express = require('express');
var app = express();
var PORT = 3000;
 
app.get('/', function (req, res) {
  console.log(req.subdomains);
  res.send();
});
 
app.listen(PORT, function(err){
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

Steps to run the program: 
 

  1. The project structure will look like this: 
     

  1. Make sure you have installed express module using the following command: 
     
npm install express

  1. Run index.js file using below command: 
     
node index.js

  1. Output: 
     
Server listening on PORT 3000


  1.  
  2. Now open your browser and go to http://localhost:3000/ , now you can see the following output on your console: 
     
Server listening on PORT 3000
[]


  1.  

Example 2: 
But if we had any other Host like car.lambo.example.com, then the output would be as shown below: 
 

['car', 'lambo']


Reference: Offical Documentation
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :