Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Extracting part of the URL

In my node.js server i have a URL variable.

It is either in the form of "https://www.URL.com/USEFUL_PART/blabla", or "URL.com/USEFUL_PART/blabla".

From this, i want to extract only the 'USEFUL_PART' information.

How do i do that with Javascript?

I know there are two ways to do this, one with vanilla js and one with regular expressions. I searched the web but i only found SO solutions to specific questions. Unfortunately, i coulnd't find a generic tutorial i could replicate or work out my solution.

like image 871
user1584421 Avatar asked Oct 17 '25 11:10

user1584421


1 Answers

Since you're using Express, you can specify the part of the URL you want as parameters, like so:

app.get('/:id/blabla', (req, res, next) => {
  console.log(req.params); // Will be { id: 'some ID from the URL']
});

See also: https://expressjs.com/en/api.html#req.params

like image 123
Brad Avatar answered Oct 19 '25 02:10

Brad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!