in node.js with express how to remove the query string

To remove the query string in a Node.js application using Express, you can utilize the url.parse() method provided by the built-in url module. Here are the steps to achieve this:

  1. Import the required modules:
const express = require('express');
const url = require('url');
  1. Create an instance of the Express application:
const app = express();
  1. Define a route handler for the desired endpoint:
app.get('/example', (req, res) => {
  // Your code to remove the query string will go here
});
  1. Within the route handler, use the url.parse() method to parse the request URL:
const parsedUrl = url.parse(req.url);
  1. Extract the path from the parsed URL:
const path = parsedUrl.pathname;
  1. Use the res.redirect() method to redirect the user to the modified URL:
res.redirect(path);

Here's the complete code example:

const express = require('express');
const url = require('url');

const app = express();

app.get('/example', (req, res) => {
  const parsedUrl = url.parse(req.url);
  const path = parsedUrl.pathname;
  res.redirect(path);
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

This code sets up an Express server that listens on port 3000. When a GET request is made to the /example endpoint, it removes the query string from the URL and redirects the user to the modified URL.

Please note that this code assumes you have already installed Express and the required dependencies. You can install Express by running npm install express in your project directory.