These are chat archives for 75lb/local-web-server

17th
Jun 2016
krigton
@krigton_twitter
Jun 17 2016 08:16
hello
Lloyd Brookes
@75lb
Jun 17 2016 08:17
Hi
krigton
@krigton_twitter
Jun 17 2016 08:17
is there a way I can change the response headers?
I'm trying to use the mock but I'm not sure is the right way
it will hijack all the response and I'm not able to change only the things I want to :D\
I need to delete some response headers for certain urls
Lloyd Brookes
@75lb
Jun 17 2016 08:21
The mocks are for creating mock responses, if you are working with real responses you will probably need to add your own custom middleware
krigton
@krigton_twitter
Jun 17 2016 08:21
:(
Lloyd Brookes
@75lb
Jun 17 2016 08:21
It is not hard, i will show you
krigton
@krigton_twitter
Jun 17 2016 08:21
I wanted to avoid using another http-proxy thing
Lloyd Brookes
@75lb
Jun 17 2016 08:21
In v2 of the app it will be even easier
Oh, you are modifying the proxied responses?
krigton
@krigton_twitter
Jun 17 2016 08:22
yes
I'm using your server on my local machine
and for apis I'm using the rewrite
and for some routes I would like to change the response header
set-cookie
Lloyd Brookes
@75lb
Jun 17 2016 08:23
I see
krigton
@krigton_twitter
Jun 17 2016 08:23
so I like the ws cause I can very simple setup the proxy rewrites
but I also need to change some headers
oh but I see you are using the http-proxy yourself
so I could create a version of the ws for myself
and add directly the changes in that code
Lloyd Brookes
@75lb
Jun 17 2016 08:25
Yes, you could fork the project if you like
I am planning to remove http-proxy soon
Because it's terrible
krigton
@krigton_twitter
Jun 17 2016 08:25
is there something better?
is it?
Lloyd Brookes
@75lb
Jun 17 2016 08:26
It's full with bugs - see their issue list
krigton
@krigton_twitter
Jun 17 2016 08:26
I see
Lloyd Brookes
@75lb
Jun 17 2016 08:26
Most of my problems are with that lib
So i will write my own proxy lib, soo
Soon
krigton
@krigton_twitter
Jun 17 2016 08:26
sounds good :D
Lloyd Brookes
@75lb
Jun 17 2016 08:27
Http-proxy creates the responses that you need to modify
I will take a look in 5-10 mins
When i get to my deak
Desk
krigton
@krigton_twitter
Jun 17 2016 08:27
thank you
I'll look into it myself as wel
Lloyd Brookes
@75lb
Jun 17 2016 08:31
at this point , the response object is passed into http-proxy
local-web-server has no further involvement from that point..
http-proxy responses can be modified
krigton
@krigton_twitter
Jun 17 2016 08:48
I guess we can do something like
in the rewrite
use something like "req-header" instead of "from"
in the same way
or "resp-header"
krigton
@krigton_twitter
Jun 17 2016 09:03
oh I see now If the request is proxied there is no longer koa used
Lloyd Brookes
@75lb
Jun 17 2016 10:06
yes, http-proxy takes full control of the response @krigton
in the next version i will fix all these proxy issues.
krigton
@krigton_twitter
Jun 17 2016 11:23
thanks. I'm trying to sort it out myself as well cause I need this feature. otherwise I cannot use ws. I'm trying to alter the headers in your middleware.js but no success
I cannot overwrite the res.writeHead. It just not working
is there a way to alter the headers after this line app.use(_.all(route.from, mw.proxyRequest(route, app)))?
Lloyd Brookes
@75lb
Jun 17 2016 11:27
no
because that line delegates processing of the response to http-proxy
krigton
@krigton_twitter
Jun 17 2016 11:28
yes, that was my thought as well
Lloyd Brookes
@75lb
Jun 17 2016 11:28
from that point onward, the response is created and sent inside the http-proxy lib
krigton
@krigton_twitter
Jun 17 2016 11:28
but If I use res.writeHead
it freezes
goes to pending
I don't think I have to create a new server
in the middleware
Lloyd Brookes
@75lb
Jun 17 2016 11:29
i will have a quick look myself, now
krigton
@krigton_twitter
Jun 17 2016 11:29
I'm trying to overwrite the this.res.writeHead
I just a need a way to alter the headers somehow I will do the rest then myself
Lloyd Brookes
@75lb
Jun 17 2016 11:33
i think you'll need to do that in http-proxy code.. did you check this too https://github.com/nodejitsu/node-http-proxy#modify-a-response-from-a-proxied-server
krigton
@krigton_twitter
Jun 17 2016 11:37
yes but do not really understand it, how to integrate that code with your proxy.web?
Lloyd Brookes
@75lb
Jun 17 2016 11:41
same here, i haven’t looked into it yet.. taking a quick look now
krigton
@krigton_twitter
Jun 17 2016 11:42
thanks
Lloyd Brookes
@75lb
Jun 17 2016 11:59
i think i did it
krigton
@krigton_twitter
Jun 17 2016 11:59
I did somthing like this for test
if(this.res.getHeader('Set-cookie')) {
      var that = this;
      var oldWriteHead = this.res.writeHead;

      this.res.writeHead = function() {

          that.res.setHeader('egy', that.res.getHeader('Set-cookie'));

          oldWriteHead.apply(this, arguments);
        }
    }
in the promise
Promise
I think I can use the arrow function
Lloyd Brookes
@75lb
Jun 17 2016 12:00
i found a way to modify the response
at this line add this code:
proxy.on('proxyRes', function (proxyRes, req, res) {
    res.setHeader('Set-Cookie', ['type=ninja', 'language=javascript'])
  })
krigton
@krigton_twitter
Jun 17 2016 12:00
wow that's an elegant solution
Lloyd Brookes
@75lb
Jun 17 2016 12:01
inside that event listener, you can modify the res directly
hopefully it helps
krigton
@krigton_twitter
Jun 17 2016 12:01
proxy.once('proxyReq', function (proxyReq) {
proxyReq.path = url.parse(route.new).path
})
is like this
oki I give a try
thanks
very much
Lloyd Brookes
@75lb
Jun 17 2016 12:02
that is a listener for proxyReq.. you need to add a listener for proxyRes
as i pasted above
welcome
krigton
@krigton_twitter
Jun 17 2016 12:02
yes
krigton
@krigton_twitter
Jun 17 2016 12:59
do you think we can extend the master version with this functionality using the module idea from the mock
like if(route.module) then route.module = require the module
then in the middleware check if there is a module.response and request
and just simply attach them to the proxyReq and proxyRes events
i'm testing this now
Lloyd Brookes
@75lb
Jun 17 2016 13:19
yes, we should do that.
krigton
@krigton_twitter
Jun 17 2016 13:19
I will make a pull request latter on
Lloyd Brookes
@75lb
Jun 17 2016 13:19
cool, thanks
krigton
@krigton_twitter
Jun 17 2016 13:20
I thank you