grunt-proxy icon indicating copy to clipboard operation
grunt-proxy copied to clipboard

Issue hitting grunt-proxy

Open srmelody opened this issue 10 years ago • 5 comments

Hey there, I see this issue after adding grunt-proxy. When express is reloaded by grunt or before grunt serve boots express and I make an HTTP request before the express loading is done, I get this error:

/Users/smelody/repos/repo/node_modules/grunt-contrib-compass/node_modules/tmp/lib/tmp.js:261
  throw err;
        ^


Error: Can't set headers after they are sent.
  at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
  at ServerResponse.res.setHeader (/Users/smelody/repos/proj/node_modules/karma/node_modules/connect/lib/patch.js:63:22)
  at ServerResponse.<anonymous> (/Users/smelody/repos/proj/node_modules/karma/node_modules/connect/lib/patch.js:80:14)
  at Array.forEach (native)
  at ServerResponse.res.writeHead (/Users/smelody/repos/proj/node_modules/karma/node_modules/connect/lib/patch.js:79:28)
  at Socket.proxyError (/Users/smelody/repos/proj/node_modules/grunt-proxy/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:193:9)
  at Socket.g (events.js:175:14)
  at Socket.EventEmitter.emit (events.js:117:20)
  at net.js:441:14

srmelody avatar Jul 09 '14 20:07 srmelody

Could you provide a sample Gruntfile to reproduce an error?

tutukin avatar Jul 10 '14 16:07 tutukin

I see this when I have a gruntfile that includes an express:dev tasks that performs a long operation (in this case, loading tons of sample data into the database). Before express boots, if I make a request, I will get that error.

 grunt.registerTask "serve", (target) ->
    if target is "dist"
      return grunt.task.run([
        "build"
        "express:prod"
        "open"
        "express-keepalive"
      ])
    if target is "debug"
      return grunt.task.run([
        "clean:server"
        "bower-install"
        "concurrent:server"
        "autoprefixer"
        "concurrent:debug"
      ])
    grunt.task.run [
      "clean:server"
      "bower-install"
      "concurrent:server"
      "proxy"
      "autoprefixer"
      "express:dev"
     # TODO - this is only working when you wait until express is booted all the way before making a request.
     # "open"
      "watch"
    ]
    return

srmelody avatar Jul 15 '14 17:07 srmelody

I was seeing this error too. I suspect it may be fixed in a newer version of http-proxy. https://github.com/nodejitsu/node-http-proxy/pull/388

bcbailey avatar Sep 29 '14 21:09 bcbailey

@bcbailey thanks for the catch. I tried upgrading this today, but I had been using the router option and router is now deprecated. I pushed some hacky commits to https://github.com/srmelody/grunt-proxy/tree/upgrade-http-proxy but I'm a little lost as to how to upgrade and convert the API. node-http-proxy published this post about using the new API as a router.

https://blog.nodejitsu.com/node-http-proxy-1dot0/

srmelody avatar Sep 30 '14 13:09 srmelody

proxy: localhost: options: host: 'localhost' port: 9000 xfwd: true https: getCertConfig('server')

      router:
        'localhost/slm': 'https://test13cluster:443/slm',
        'localhost/': 'http://127.0.0.1:4000'    # This needs to be last because it greedily matches.


      changeOrigin: true

  '10.0.0.2':
    options:
      host: 'localhost'
      port: 9001

      https: getCertConfig('10.0.0.2')

      router:
        '10.0.2.2/slm': 'https://test13cluster:443/slm',
        '10.0.2.2/': 'http://127.0.0.1:4000'

      changeOrigin: true

srmelody avatar Sep 30 '14 13:09 srmelody