Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy with mapUri, example #599

Closed
gregtzar opened this issue Feb 28, 2013 · 5 comments · Fixed by #602
Closed

proxy with mapUri, example #599

gregtzar opened this issue Feb 28, 2013 · 5 comments · Fixed by #602
Assignees
Labels
bug Bug or defect documentation Non-code related changes
Milestone

Comments

@gregtzar
Copy link

Would it be possible to provide an example of using the proxy handler with mapUri? I am trying to implement a /proxy route which performs a simple forward proxy to the url querystring which gets passed to the route. I thought that mapUri would do this, but am left guessing at how it works, and what the method signature is (I just didn't have time to dig into the source today but will try that tomorrow). I must be using it wrong because the literal value for the url querystring is getting returned to the client.

Example server:

var port = 9090,
    Hapi = require('hapi'),
    server = new Hapi.Server('localhost', port);

server.route({
    method: "GET",
    path: "/proxy",
    handler: {
        proxy: {
            mapUri: function(req, cb) {
                cb(req.query.url);
            }
        }
    }
});

server.start();

Example request:

GET http://localhost:9090/proxy?url=http://www.google.com

Example response:

http://www.google.com

Expected response:

[google homepage]
@ghost ghost assigned hueniverse Feb 28, 2013
@hueniverse
Copy link
Contributor

The current mapUri callback signature makes no sense. I'll update it shortly and document the pattern. Your code above should work with a minor tweak:

cb(null, req.query.url);

@gregtzar
Copy link
Author

Right on. So with this contract change, would the mapped querystring now become an optional part of the mapped url (as opposed to being a separate parameter passed to the callback)?

@hueniverse
Copy link
Contributor

Yes, you just need to pass back a fully qualified URI

@maimike
Copy link

maimike commented Apr 5, 2019

This example no longer works. Got error cb is not a function even i use cb(null, req.query.url);

@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect documentation Non-code related changes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants