Git Product home page Git Product logo

dev-proxy's Introduction

Development proxy

A simple proxy server to help you proxy your REST api calls to a web server running on the same machine, but another port.

Description

Do NOT use dev-proxy at the same time as the body-parser middleware, because the body-parser will eat the payload body from all your POST and PUT requests. GETs will, however, work just fine.

Are you using some form of an apache web server, such as tomcat, to serve your REST api while doing your front-end development work in Meteor?

If so, then you might encounter these two problems:

  1. The apache server serves on a different port than your app, and so you can't just call your REST apis using the relative form /api/dosomething

  2. CORS restrictions might prevent you from making any REST calls from the Meteor based client side to the Apache based REST server, because the apache server uses a different port than Meteor's. For example, apache servers commonly use localhost:8080, whereas Meteor's default setup serves to localhost:3000.

This proxy will recognise calls to certain routes on your local Meteor server and send then to the other (possibly apache) server. For example

localhost:3000/rest/

can be sent to

localhost:8080/rest/

It is up to you to ensure that the proxy is enabled only when you need it.

For example, once you deploy to production, where your front-end code is served by apache or some other server, the proxy may no longer be necessary. To ensure that the DevProxy is enabled only in development you could use code like this:

if (process.env.NODE_ENV === "development" && typeof DevProxy !== 'undefined') {
    DevProxy.addProxy( .... )
}

Installation

meteor install pbastowski:dev-proxy

Usage

This package exports DevProxy, which has the following method that allows you to configure the proxying:

DevProxy.addProxy(source, target, replacePath);

Or, if you have more than one server that you want to proxy to, then you could do this:

DevProxy.addProxy([
  // proxy all requests for "/myapp/img/" to "http://localhost:3000/img" (removing "/myapp")
  {
    source: "/myapp/img",
    target: "http://localhost:3000",
    replacePath: { search: "/myapp/", replace: "/" }
  },

  // proxy all requests for "/myapp/api/" to "http://localhost:8080/myapp/api"
  { source: "/myapp/api/", target: "http://localhost:8080" },

  // proxy only when the URL path is the exact string "/goo"
  {
    source: "/goo",
    target: "https://www.google.com.au",
    exactMatch: true,
    replacePath: { search: "/goo", replace: "/" }
  }
]);

The example above also shows how to cater for the eventual deployment to a server that will require a prefix, such as "myapp", in the URL after the server name. So, you may be developing and testing on localhost:3000/home, but the target server may require an app name prefix, such that the URL will become my.server.com/myapp/home. To fix this, replacePath is used to remove the "myapp" prefix from the URL. In this way you won't have to make any changes to your app when you deploy it to the server.

  • source is the source path of the URL on your Meteor server that needs to be proxied to the target server. Proxying will trigger if the URL on the server contains the source string.
  • target is the target server URL that you want to forward your requests to.
  • replacePath is optional. With replace path you can specify that a portion of your source path shoudl be replaced with a new string. For example if the /rest/ path on your server is equivalent to the /api/ path on the target, then replace path would look like this: { search: '/rest', replace: '/api' }. search and replace can be any valid URL strings.
  • exactMatch is optional. When true the URL on the Meteor server must match exactly the source string to activate the proxying.
  • matcher is optional. It has no effect if exactMatch = false . It is the match function to be used. It can be used, for example, to make regular expression matcher or to negate a expression. If not informed, the following function will be used:
function defaultMatcher(reqUrl, source) {
  return reqUrl.indexOf(source) > -1;
}

For example, the configuration below will proxy all calls made to /rest/ to http:localhost:8080/rest/.

if (process.env.NODE_ENV === "development" && DevProxy) {
  DevProxy.addProxy("/rest/", "http://localhost:8080", {
    search: "/rest",
    replace: "/api"
  });
}

Note: If the proxying is not working for you, try using google.com as the target. Google will display an error message to the user, including the requested path, when it receives a request for a path that it does not recognise. This may help you identify the problem.

Changelog

31 Oct 2018 - v0.0.7

  • Adding support for Meteor v1.8.

4 Oct 2017 - v0.0.6

  • Updated http-proxy to v1.16.2
  • Added matcher option, see Usage section above.

1 Jul 2016 - v0.0.5

  • Updated http-proxy to v1.14.0

dev-proxy's People

Contributors

lucas-issa avatar pbastowski avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

lucas-issa docoyo

dev-proxy's Issues

Matching an exact route, not a pattern

I have a question.

What if I would like to proxy /foo, but not /foo/bar ?
In other words, how do I tell the pattern recognizer not to interpret the source parameter as a pattern,
but as a string that must be an exact match?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.