Node.js Essentials: url
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: Returnshttporhttpsdepending 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).




