How to get full URL in Express.js ?
Last Updated :
24 Jul, 2024
Express is a small framework that sits on top of Node.js’s web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. It adds helpful utilities to Node.js’s HTTP object and it facilitates the rendering of dynamic HTTP objects.
Use the following steps to install the module and get the full URL in Express.js:
Step 1: Creating a directory for our project and making that our working directory.
$ mkdir demo
$ cd demo
Step 2: Use the npm init command to create a package.json file for our project.
$ npm init

Note: Keep pressing enter and enter “yes/no” accordingly at the terminus line.
Step 3: Installing the Express.js module. Now in your demo(name of your folder) folder type the following command line:
$ npm install express --save

Step 4: Creating index.js file, our Project structure will look like this.

Step 5: Creating a basic server. Write down the following code in the index.js file.
index.js
const express = require('express');
const app = express();
app.get('/' , (req , res)=>{
res.send("GeeksforGeeks");
})
// Server setup
app.listen(4000 , ()=>{
console.log("server is running on port 4000");
})
Output: We will get the following output in the browser screen.
GeeksforGeeks
Step 6: Getting the full link as a response to a request. Here, For the full link, we are going to use protocol, hostname, and original URL which is present in the request object.
index.js
const express = require('express');
const app = express();
app.get('/' , (req , res)=>{
res.send("GeeksforGeeks");
});
app.get('/gfg' , (req , res) => {
// Creating Full Url.
var fullLink = req.protocol + "://" +
req.hostname + req.originalUrl;
res.send(fullLink);
});
// Listening App
app.listen(4000 , ()=>{
console.log("server is running on port 4000");
});
Step 7: Run the server using the following command.
node index.js
Output:
Similar Reads
How to Get the Full URL in ExpressJS ? When building web applications with Express.js, a popular framework for Node.js, there are many scenarios where you may need to retrieve the full URL of a request. This is especially useful for tasks like logging requests, generating absolute URLs for redirect responses, or creating links in respons
6 min read
How to use get parameter in Express.js ? Express Js is a web application framework on top of Node.js web server functionality that reduces the complexity of creating a web server. Express provides routing services i.e., how an application endpoint responds based on the requested route and the HTTP request method (GET, POST, PUT, DELETE, UP
2 min read
How to use Global functions in Express JS? In this article, we will learn the global function of Express. Express JS is a web application framework for Node JS. This framework runs on the server-side framework. It is a trendy Express JS framework for building scalable web applications. There are many functions available in Express JS that ar
2 min read
How to handle URL parameters in Express ? In this article, we will discuss how to handle URL parameters in Express. URL parameters are a way to send any data embedded within the URL sent to the server. In general, it is done in two different ways.Table of ContentUsing queriesUsing Route parameterSteps to Create Express Application:Let's imp
3 min read
How to Use Handle Get Request in Express.js ? Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in
2 min read