1

Consider a url like this:

http://some-site.com/something/http://www.some-other-site.com

I am trying to log to the console the bold part from the query string i.e. the second http:// using the following method.

app.get("/something/:qstr",function(req,res){

    console.log(req.params.qstr);   
};

However this will only work until the http: --> as soon as the // is encountered it is no longer included in the req.params.qstr I'd like to know how to get the entire URL string. How can I achieve this?

Thank you.

9
  • That's not a valid URL; the URL component in the path would have to be URL encoded. That may be the reason why it's not working for you. Where are you getting this data from? Commented Jan 27, 2017 at 9:17
  • What I am trying to accomplish is to save the second URL to my db whenever it is entered after something, I am trying to extract that part and save to my db. Commented Jan 27, 2017 at 9:18
  • But why do you receive URLs that are structured in this broken way in the first place? Commented Jan 27, 2017 at 9:21
  • It's a project I am working on where you can go to my website i.e. some-site.com navigate to the /something/ route... Enter a URL in proper format, and once it is entered it will save to the Db. Do you know how to extract the URL here? Commented Jan 27, 2017 at 9:23
  • Use the expression: /something/:qstr(.*) Commented Jan 27, 2017 at 9:26

1 Answer 1

2

You can try this, using a regex:

var app = require('express')();

app.get(/^\/something\/(.*)/, function (req, res) {

    console.log(req.params[0]);
    res.json({ok: true});

});

app.listen(3333, () => console.log('Listening on 3333'));

When you run:

curl http://localhost:3333/something/http://www.some-other-site.com

the server prints:

http://www.some-other-site.com

as you wanted.

The res.json({ok: true}); is there only to return some response so the curl will not hang forever.

Sign up to request clarification or add additional context in comments.

5 Comments

you can change res.json to res.send() and you'll get a 200 back
This solved it thanks. can you please quickly explain the regex? It's just saying grab everything until after the slash right?
If I do app.get() it will work however if do the same thing with app.use it will not. app.use(/\/new\/(.*)/, error); - error.js --> router.get("/",function(req,res){ console.log(req.params[0]) res.send(req.params[0]) }); please advise
@JohnNoob I updated the regex to be /^\/something\/(.*)/ which means - anything that starts with /something/ and then anything captured as req.params[0]. app.use is different than app.get - see: expressjs.com/en/guide/using-middleware.html
Yes thank you, I'm comfortable with JS but new to Node so still finding my way around. I aspire to be as knowledgeable with Node as you one day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.