JohnSnow
JohnSnow

Reputation: 7111

Extracting the URL from the query string

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.

Upvotes: 1

Views: 77

Answers (1)

rsp
rsp

Reputation: 111268

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.

Upvotes: 2

Related Questions