http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.87k stars 1.96k forks source link

how to send programmatic error #1193

Open victorbucutea opened 7 years ago

victorbucutea commented 7 years ago

Hello,

I have certain cases in which I have some security checks in modifying the path of the original http request. Those security checks might fail and I do a :

proxy.on('proxyReq', function(proxyReq, req, res, options) { res.status(403).send("You are not permitted to access...");

This type of shortcut works in middleware, but I've noticed that node-http-proxy will still attempt to call the target url and write the proxy response to the initial response.

How to short-circuit the response in a 'proxyReq/proxyRes' event?

victorbucutea commented 7 years ago

The answer to this is calling proxyReq.abort() . Is it safe to just add this line after the res.status(403).send.. ?