gonzalocasas / node-proxy-middleware

proxy as middleware
MIT License
146 stars 65 forks source link

URL pattern matching #34

Closed nmehta6 closed 9 years ago

nmehta6 commented 9 years ago

It would be nice to be able to use patterns for URLs so that this proxy is a little more flexible. Instead of string comparison, minimatch can be used.

nmehta6 commented 9 years ago

Found a package (connect-modrewrite) that allowed me to do this.

binarykitchen commented 9 years ago

@nmehta6 how do you use connect-modrewrite in conjunction with node-proxy-middleware?

I want to add logic to ignore parts of URLs, for example

  proxyOptions              = url.parse(config.api)
  proxyOptions.preserveHost = true
  proxyOptions.route        = '/api'

but when URL is /api/system I want to ignore (skip) the proxy. That's only doable with a regex I guess. Any clues?

nmehta6 commented 9 years ago

@binarykitchen I actually swapped out node-proxy-middleware with connect-modrewrite. Here is an example of how I am doing something similar. For these regex paths I am skipping the proxy and allowing the request to go through. https://github.com/CaryLandholt/fatarrow/blob/master/tasks/server/server.coffee#L28

Let me know if you need help translating the source from CoffeeScript to js.

binarykitchen commented 9 years ago

hmmm, i tried but doesn't work well for me. here the config:

  PROXY_CONFIG = [
    "^/api/$ #{config.api} [P, L]"
  ]

  PROXY_EXCLUSIONS = [
    '^/api/system/motd*$ - [L]'
  ]

  modRewriteConfig = PROXY_CONFIG.concat(PROXY_EXCLUSIONS)

console.log(modRewriteConfig) shows

[ '^/api/$ http://localhost:8181/api [P, L]',
  '^/api/system/motd*$ - [L]' ]

i want all api calls to be redirected to port 8181, for example http://localhost:3000/api/login should become http://localhost:8181/api/login

except /api/system/motd_23423432.html shouldnt get redirected.

any clues what i am doing wrong here?

nmehta6 commented 9 years ago

I believe you have the wrong order here. First line in the config array will proxy everything.