Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express.js request.url is always /

I just created my first app with node.js and express. And I only want to print out current url, so I add these lines:

app.use('/test', function(request, response){
    console.log('current url is '+request.url);
    response.end();
})

Now when I run on browser at localhost:3000/test in will print me: current url is / Can someone explain it to me?

like image 647
Honza Soused Pospíšil Avatar asked Oct 18 '25 16:10

Honza Soused Pospíšil


2 Answers

i had the same issue and the solution was so obvious...

I had a "express rule" like:

app.all('*', function(req, res) {
    res.redirect('/')
})

And that redirect me automaticly to "/" route so it doesn't work...

In your case, I think the problem comes from your keywork "use" used with express. Try this:

app.get('/test', function(request, response){
    console.log('current url is '+request.url);
    response.end();
})
like image 193
Axel Paris Avatar answered Oct 20 '25 06:10

Axel Paris


Try for this:

var Url = req.protocol + '://' + req.get('host') + req.originalUrl;

Reference in StackOverflow: How to get full URL in Express.js

like image 38
Subburaj Avatar answered Oct 20 '25 05:10

Subburaj



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!