Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass a url as a "url parameter" in express?

In my express app I have a router listening to api/shorten/:

    router.get('api/shorten/:longUrl', function(req, res, next) {
        console.log(req.params.longUrl);
    }

When I use something like:

http://localhost:3000/api/shorten/www.udemy.com

I get www.udemy.com which is what I expect.

But when I use:

http://localhost:3000/api/shorten/http://www.udemy.com

I get a 404 error.

I want to get http://www.udemy.com when I access req.params.parameter.

like image 362
Abdelaziz Mokhnache Avatar asked Oct 27 '25 22:10

Abdelaziz Mokhnache


1 Answers

I'm not sure if you're still looking for a solution to this problem. Perhaps just in case someone else is trying to figure out the same thing, this is a simple solution to your problem:

app.get('/new/*', function(req, res) {

  // Grab params that are attached on the end of the /new/ route
  var url = req.params[0];

This way you don't have to sweat about any forward slashes being mistaken for routes or directories, it will grab everything after /new/.

like image 119
Allan of Sydney Avatar answered Oct 29 '25 13:10

Allan of Sydney



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!