Skip to main content

Command Palette

Search for a command to run...

Node.js Essentials: url

Published
โ€ข1 min read
A

A passionate MERN Stack Developer from India

I am a full stack web developer with experience in building responsive websites and applications using JavaScript frameworks like ReactJS, NodeJs, Express etc.,

Constructing and Logging the Full URL

of an Incoming Request in Node.js

http://localhost:3000/api/v1/users?page=2

const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;

console.log("Full URL: ", fullUrl);
console.log("Host: ", req.get('host'));
console.log("Original URL: ", req.originalUrl);

Explanation

  • req.protocol: Returns http or https depending on the request protocol.

  • req.get('host'): Provides the host header from the request, typically including the domain and port (e.g., localhost:3000).

  • req.originalUrl: Gives you the path of the URL along with any query parameters (e.g., /api/v1/users?page=2).

29 views

More from this blog

B

Backend Notes - Web Development

31 posts

A passionate MERN Stack Developer from India

I am a full stack web developer with experience in building responsive websites and applications using JavaScript frameworks like ReactJS, NodeJs, Express