How to get the Request Host in express.js?

This post will show you how to get a request Host in express.js.

Related Articles:

1- What is Request Host?

The Request Host header specifies the host and port number of the server to which the request is being sent.

Host header includes two parts: scheme host and port :

 Host: <host>:<port>

<host>

host: is the domain name of the server.

port: The port is optional and contains the TCP port number on which the server is listening. When no port is included, the default port is 443 for an HTTPS URL, and 80 for an HTTP URL).

Example:

  Host: hostExample.com

2- Get the request Host from HOST header

To get the Request Host from the headers in expess.js, add the following lines of code to the request:

   app.use((req, res, next) => {

      const host = req.header('Host');

  });

 

OR you can use:

   
   app.use((req, res, next) => {
    
       const host = req.headers.host;

 });

 

You might also like:

How to get Request Origin in Express.js?

Node.js + MySQL : Add Forgot/Reset Password to Login-Authentication System.

How to Build a Complete API for User Login and Authentication using MySQL and Node.js.

How to store Session in MySQL Database using express-mysql-session.

How to interact with MySQL database using async/await promises in node.js ?

How to use Sequelize async/await to interact with MySQL database in Node.js.

MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.

ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js

ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.

How to add Routes to insert data into MySQL database-related tables in Node.js API?

Example How to use initialize() Function in Node.js/Express API .

Why is Connection Pooling better than Single Connection?.

How to create MySQL database using node.js.

Leave a Comment

Your email address will not be published.

Translate »