express-http-proxy icon indicating copy to clipboard operation
express-http-proxy copied to clipboard

Responding immediately to client without forwarding to proxy

Open sscarduzio opened this issue 3 years ago • 1 comments

Sometimes, for a certan combination of method, path, header, I would like to bounce back the request BEFORE it's forwarded to the server.

I'm no sure where to create this response object, and how to return it so that the request never reaches the server.

I.e.

const proxy = require('express-http-proxy');
const app = require('express')();

const port = 5000

app.use(proxy('localhost:9200', {
  https: true,
  preserveHostHdr: true,
  parseReqBody: false,

  proxyErrorHandler: function (err, res, next) {
    if (err) console.log("ERROR!  ", err)
    next(err);
  },

  proxyReqPathResolver: function (req) {
    console.log("> "+req.method + " ",req.url)
    return req.url
  },
  
  proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
    // letting self signed ssl cert go through
    proxyReqOpts.rejectUnauthorized = false
    
    if(srcReq.path.indexOf("siem")>=0 && srcReq.method == 'PUT' && srcReq.statusCode == 400){
      console.log("responding immediately with a 200...")
       // return ???????? <--- I don't want this request to reach the server!
    }
    return proxyReqOpts;
  }

}));

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

sscarduzio avatar Aug 02 '20 20:08 sscarduzio

Is this possible? Also looking for this. Thanks!

bilics avatar Feb 16 '22 21:02 bilics