Http-proxy-middleware: How to: Cancel request before proxying to external resource

Created on 27 Apr 2017  路  4Comments  路  Source: chimurai/http-proxy-middleware

Intro/Overview

Hi, I have created a proxy utility which will both proxy to a remote (internal server) and record the response from the server into a folder structure which matches the endpoint requested. In order to accomplish this I needed to be able to get through the corporate proxy, so I add customer headers and used https-proxy-agent following the example to do the handshake with the corporate proxy. I am currently trying to add a useCache mode to the proxy utility. I would like this mode to cancel any requests at the proxy and return the locally stored data if the matching file for the endpoint exists.

Current Hurdle

When trying to cancel the request in the onProxyReq event I receive an error which reads Error: Can't set headers after they are sent..

Question

Is there a way to cancel the request at the proxy so that I can return the content of a file in the response without making the real request?

Update

I was able to cancel the request successfully using proxyReq.abort but whenever a request is aborted it is throwing an error that reads [HPM] Error occurred while trying to proxy request. Is there a way to prevent this error from occurring?

All 4 comments

I figured out how to accomplish this by creating a custom middleware that runs before the proxy middleware.

Thanks for sharing your solution @folterung.
Think I would've done the same.

@folterung may I ask how to create a custom middleware that runs before the proxy middleware?

@chimurai it seems very odd that鈥檚 calling abort crashes the whole server, at least in my case. I'm using Nuxt. Is this expected?

It looks like I'll have to go the middleware path which is was I was expecting.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kimmobrunfeldt picture kimmobrunfeldt  路  5Comments

ubreddy picture ubreddy  路  5Comments

gajus picture gajus  路  8Comments

lukaszpochrzest picture lukaszpochrzest  路  3Comments

zh-h picture zh-h  路  3Comments