Implementing dynamic routing to manage the path /a/b-c-d-:e-:f

Here is a sample route:

server.get("/something/best-shoes-in-india-:brand-:location", (req, res) => {
   res.send(JSON.stringify(req.params))

})

For example, if brand name = adidas and location = Delhi:

If the URL is => "/something/best-shoes-in-india-adidas-delhi it returns => { brand: adidas, location: delhi } which is correct. However,

If the brand name is => adi das, and location = delhi then the URL becomes => "/something/best-shoes-in-india-adi-das-delhi

Now it returns => { brand: adi, location: das-delhi }

How can we achieve "adi-das" as the brand name in this case?enter code here

Answer №1

This is how you should handle it.

server.get("/find/best-shoes-in-india-/:brand/:city", (req, res) => {
  const { brand, city } = req.params;
  console.log(brand, city);
  res.send(JSON.stringify(req.params))
});

The brand will be stored in req.params.brand The city will be stored in req.params.city

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Managing npm packages installed globally

I am really struggling to understand how node's globally installed packages behave. When I install a package like http-server globally, I can run it simply by typing http-server. http-server However, if I try to run it with node http-server, I encoun ...

Ways to retrieve an object from a separate function in JavaScript

I am struggling to fetch data from an array and store it in an object. However, every time I try, I end up with either an empty object or a Promise { <pending> } shown in the logs. I have attempted to use a global variable to store the data and acc ...

Interactive Zoomable Tree with d3.js

I am looking to customize the zoomable icicle plot in d3js by incorporating my own data. Unfortunately, I am unable to locate the "readme.json" file for data modification and cannot get the graph to display on my local machine. Where can I find this elus ...

Bring in NPM package that relies on another module

Recently transitioning to Meteor 1.3 with npm module support, I've encountered the following issue: TypeError: Cannot set property 'tip' of undefined Below is the relevant code snippet in myFile.js: import d3 from 'd3'; import d ...

Conceal only the anchor tag's text and include a class during the media query

In the anchor tag below, I have a text that says "Add to cart," but on mobile view, I want to change it to display the shopping cart icon (fa fa-cart). <a class="product"><?php echo $button_add_to_cart ?></a> Currently, the variable $bu ...

Encountering the ERR_SSL_PROTOCOL_ERROR browser message when operating a Node.js Express server with HTTPS

I've set up multiple nodejs applications running on Express with the following code. They all work smoothly with similar code to this: fs = require 'fs' https = require 'ht ...

The "sleep mode" feature in the Messenger Bot aims to optimize the getaddrinfo function

Recently, I have been facing an issue with the HTTP requests made by a bot developed in nodejs (with expressjs) that is hosted on my private VPS. The problem arises when there is a long period of inactivity, causing the nodejs server to go into a sort of " ...

Error message in React: "Module './assets/photo.jpg' not found"

Encountering an error that says "Cannot find module './assets/photo.jpg'". This is the code snippet in app.js import ReactDOM from 'react-dom'; import './App.css'; import Intro from './components/Introduction' func ...

Deciphering the intricate mechanics behind _.bind

This block of code is an excerpt from the Underscore library, specifically showcasing the implementation of the _.bind function. However, I am struggling to comprehend the purpose behind modifying the prototype of an empty function. var customConstruc ...

An error occurred in the promise: Unable to access properties of an undefined object when trying to read 'img1'

I am currently working with react.js async function Banners(props) { const response = await axios.get(`${apiUrl}/assets/get`); return ( <MainContent text={response.text} img1={props.img1 ? props.img1 : response.data.img1} img2 ...

Guide on passing a JSON body as a string in a PUT API request using Fetch in a React.js application

How can I pass a JSON body as a string in a FETCH PUT API in React JS? I am attempting to add 20 to the Balance like this: Balance: details[0].Balance.toString() + 20, but it is not working as expected. I want the entire result to be a string. Any assista ...

Enhancing MaterialUI Card in React with custom expandable feature: learn how to dynamically change Card styles on expansion

There are a total of 20 cards displayed on this page. When using MaterialUI Card, the onExpandChange property allows for defining actions like this: <Card expandable={true} onExpandChange={this.clickHandle}> With this action, it is easy to deter ...

Unable to show the selected option from the dropdown menu

An input field allows users to type normally, with an arrow positioned next to the field. https://i.sstatic.net/4wJmw.png Clicking on the arrow opens a dropdown menu containing a list of data options. https://i.sstatic.net/0GGku.png When a user selects ...

Having trouble getting my images to load in React. Seems like a common issue that many people run into

UPDATE: After trying different paths for my image import (such as 'import Github from '../..img/github.png'), the errors have disappeared but the image still won't load on my app. Folder Structure: My-Portfolio node_modules public src ...

Develop internal stateless functions using flowtype

Ever since the recent flow update (0.61), it feels like I've entered a never-ending nightmare. Currently, I'm facing a challenge trying to pass flow tests for a simple stateless component. Take a look at my code: function FlexRow (props: PropT ...

Is the popup successfully closed when the button is tested?

I am currently working on testing a simple pop-up to see if the clicked button closes the popup using testing-library/react. The approach in the documentation doesn't seem to be working for me, as I can successfully console log the button but fireEven ...

What could be the reason behind the error encountered while attempting to parse this particular JSON object?

I am encountering an issue with my PHP (Codeigniter Framework) code: $webpages = $this->webpageModel->select('webpageID,webpageTitle')->where('webpagecategoryID', $webpagecategoryID)->findAll(); header('Content-Type: ap ...

Is there a way to show user input text in a typing animation with additional predetermined text?

Using the prompt function, I am gathering input and storing it in variable "a". Here is the code snippet: <script type="text/javascript" language="Javascript"> var a=prompt("Please Enter Your Name ...

Ways to navigate a div within an iframe that has been loaded

As I load a page(A) inside an iframe, the HTML structure of the embedded content is as follows: <html><body> <div id="div1"></div> <div id="div2"><button>Hello</button></div> </body></html> The ...

Symbol shortcut for changing to toString

When it comes to obtaining the numeric value of a variable like $event, typically I would use the following syntax: <ax-text-box (valueChange)='documentSearchItem.fromPrice=(+$event)'></ax-text-box> To achieve this, I simply add a +. ...