villadora / express-http-proxy

Proxy middleware for express/connect
Other
1.22k stars 235 forks source link

Responding immediately to client without forwarding to proxy #464

Open sscarduzio opened 4 years ago

sscarduzio commented 4 years ago

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}`)
})
bilics commented 2 years ago

Is this possible? Also looking for this. Thanks!