Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  188] [ 1]  / answers: 1 / hits: 5541  / 7 Years ago, fri, march 24, 2017, 12:00:00

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.


More From » node.js

 Answers
3

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/.


[#21886] Thursday, March 23, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyemathewj

Total Points: 484
Total Questions: 107
Total Answers: 111

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;