Angular2/Angular seed http-proxy-middleware proxy api requests -
im using angular seed project , trying set proxy api requests backend service running on different port.
my code far:
/* add proxy middleware */ this.proxy_middleware = [ require('http-proxy-middleware')({ ws: false, target: 'http://localhost:5555', router: { // when request.headers.host == 'dev.localhost:3000', // override target 'http://www.example.org' 'http://localhost:8000' //'http://localhost:5555/basepath/api' : 'http://localhost:7000/api' } }) ];
basically need route api matching http://localhost:5555/basepath/api http://localhost:7000/api though cant seem working using http-proxy-middleware. had working using proxy-middleware have switched need modify request headers , seems can done http-proxy-middleware.
spent bit of time trying work , ended adding project.config.ts within constructor.
/* add proxy middleware */ this.proxy_middleware = [ require('http-proxy-middleware')( '/basepath/api', { ws: false, onproxyreq: this.onproxyreq, target: 'http://localhost:7000', pathrewrite: { '^/basepath/api' : '/api' } }) ];
and function included below constructor add header requests proxied
onproxyreq(proxyreq: , req: any, res: any) { // add custom headers request proxyreq.setheader('header_name', 'value'); }
Comments
Post a Comment