8

How to get url after "#" in Express Middleware Request ? - GeeksforGee...

 8 months ago
source link: https://www.geeksforgeeks.org/how-to-get-url-after-in-express-js-middleware-request/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to get url after “#” in Express Middleware Request ?

Courses

In Express, the fragment identifier (the part of the URL after the # symbol) is not directly accessible on the server side because it is not sent to the server as part of the HTTP request. The fragment identifier is typically used for client-side navigation and is processed on the client side only.

If we need to access data from the fragment identifier on the server side, we can use JavaScript on the client side to send that data to the server, either through an AJAX request or by including it in a regular HTTP request.

Prerequisites:

Approach to get URL after “#” in Express:

Client-Side (Browser):

  • Retrieve Fragment Identifier:
    • Use window.location.hash to get the fragment identifier.
    • Store the value in a variable (e.g., fragmentValue).
  • Send Data to Server (AJAX):
    • Create an XMLHttpRequest (xhr) object.
    • Open a POST request to the server endpoint (/processFragment).
    • Set the request header to ‘application/json’.
    • Send a JSON object containing the fragment value to the server.

Server-Side (Express):

  • Receive and Process Data:
    • Use Express to handle incoming POST requests at the endpoint (/processFragment).
    • Utilize the body-parser middleware to parse JSON data from the request body.
  • Access Fragment Value:
    • Retrieve the fragment value from req.body.fragment.
  • Process and Respond:
    • Process the fragment value as needed on the server side.
    • Respond to the client, indicating that the fragment value has been received and processed.

Steps to Create an Express Application:

Step 1: In the first step, we will create the new folder by using the below command in the VScode terminal.

mkdir folder-name
cd folder-name

Step 2: After creating the folder, initialize the NPM using the below command. Using this, the package.json file will be created.

npm init -y

Step 3: Now, we will install the express dependency for our project using the below command.

npm install express body-parser

Project Structure:

file-Structure

File Structure

The updated dependencies in the package.json file will look like:

"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
}

Example: Below is the code of above explained approach

  • Javascript
// index.js
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const port = 3000;
app.use(cors());
app.use(bodyParser.json());
app.post("/processFragment", (req, res) => {
const fragmentValue = req.body.fragment;
console.log('Fragment value:', fragmentValue);
// doing according to your need with the fragmented value
const responseMessage =
`Fragment value received and processed. 
\nInput Value: ${fragmentValue}`;
res.send(responseMessage);
});
app.listen(port, () => {
console.log(`Server listening 
at http://localhost:${port}`);
});

Step 4: Run the below command to start the server

node index.js

Output:

ezgifcom-crop-(7)

Output

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 03 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK