Hi,
I'm trying to do a redirect inside an EMG plug-in, but it seems it's not the way I imagined. I created the following implementation in a plugin I have here in onrequest event handler:
function _onRequest (req, res, next) { debug('plugin invoked on request.') if (req.url === ROOT_BASE_PATH) { debug('This is a call for Root Base Path') if (_redirectRootTo) { debug('_redirectRootTo ==> %s', _redirectRootTo) req.url = _redirectRootTo res.statusCode = 301 next() } else { //do something else } } else { next() } }
Also I have an api proxy in Apigee Edge routing to root base path with:
Base Path: /
target: http://errorurl
Basically when I call the root base path through EMG with this plugin enabled and with the "_redirectRootTo" providing values (this is a config from EMG config file), I want to redirect the / to a given path set in the property _redirectRootTo. Example:
In Emg config:
myplugin:
_redirectRootTo = /echo
/echo in this case is an API proxy in apigee edge. So I expect to call / root base path, then my plugin change the request from / to /echo and target to the url set as target of /echo.
However the code above does not work, I'm running this through a corporate proxy:
gateway:main selected proxy http://errorurl with base path / for request path / +7s gateway:main sourceRequest +3ms 66f67850-b9b4-11e7-9232-29fe6d60b0be GET / info sourceRequest m=GET, u=/, h=localhost:8080, r=::1:48044, i=66f67850-b9b4-11e7-9232-29fe6d60b0be plugin:myplugin plugin invoked on request. +7s plugin:myplugin This is a call for Root Base Path +0ms plugin:myplugin _redirectRootTo ==> /echo +0ms info targetRequest m=GET, u=http://errorurl/, h=web-proxy.corp.myproxy.com:8080, i=66f67850-b9b4-11e7-9232-29fe6d60b0be gateway:main targetRequest +11ms 66f67850-b9b4-11e7-9232-29fe6d60b0be GET web-proxy.corp.myproxy.com 8080 http://errorurl/ gateway:main targetResponse +468ms 66f67850-b9b4-11e7-9232-29fe6d60b0be 404 info targetResponse s=404, d=485, i=66f67850-b9b4-11e7-9232-29fe6d60b0be
As it can be seen, changing the "req.url" does not change the request and it targets the url set in my original root base path set in apigee edge which is http://errorurl.
Then, I tried to force the target changes in code to see what happened, like:
function _onRequest (req, res, next) { debug('Error mapping plugin invoked on request.') if (req.url === ROOT_BASE_PATH) { debug('This is a call for Root Base Path') if (_redirectRootTo) { debug('_redirectRootTo ==> %s', _redirectRootTo) req.url = _redirectRootTo req.targetSecure = false req.targetHostname = 'localhost' req.targetPort = 3000 res.statusCode = 301 next() } else { //do something else } } } else { next() } }
But when running I could see the the changes in the "u" to the url I just set inside the plugin, although I get:
gateway:main selected proxy http://errorurl with base path / for request path / +28s gateway:main sourceRequest +8ms 85462220-b9b3-11e7-aaaa-913d77a7d7b7 GET / info sourceRequest m=GET, u=/, h=localhost:8080, r=::1:48004, i=85462220-b9b3-11e7-aaaa-913d77a7d7b7 plugin:myplugin plugin invoked on request. +28s plugin:myplugin This is a call for Root Base Path +0ms plugin:myplugin _redirectRootTo ==> /echo +0ms info targetRequest m=GET, u=http://localhost:3000/, h=web-proxy.corp.myproxy.com:8080, i=85462220-b9b3-11e7-aaaa-913d77a7d7b7 gateway:main targetRequest +14ms 85462220-b9b3-11e7-aaaa-913d77a7d7b7 GET web-proxy.corp.myproxy.com 8080 http://localhost:3000/ gateway:main targetResponse +410ms 85462220-b9b3-11e7-aaaa-913d77a7d7b7 404 info targetResponse s=404, d=437, i=85462220-b9b3-11e7-aaaa-913d77a7d7b7
I dont understand why the target request is adding a proxy in the "h" parameter in "u=http://localhost:3000/, h=web-proxy.corp.myproxy.com:8080" and why i get a 404 response. if I call /echo directly in my curl/postman it works properly, it routes to the /echo targets normally, which in this case is http://localhost:3000.
Can anyone help me here? I though that overriding the req.url with the expected URL to redirect would be enough. Is there any particular reason why EMG is putting the proxy url in there?
Thanks!!
Ciro
User | Count |
---|---|
7 | |
2 | |
2 | |
2 | |
1 |