Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added generic proxy support to the server task. #670

Closed
wants to merge 2 commits into from

Conversation

jgrund
Copy link

@jgrund jgrund commented Oct 27, 2012

In Gruntfile.js you can add a block like this:

  proxies: {
    '^/api/v2/(.+)': function (req, res, next, match) {

    }
  }

If a request matches that regex string the associated function will be called with the standard connect parameters plus the regex match.

This provides a nice generic hook into the middleware stack, where matching paths can have custom functionality applied to their corresponding request.

 - Allows a matching path to be handled by a corresponding proxy function.
 - Can be useful for returning mocked API data, or proxying to an external source.
@sindresorhus
Copy link
Member

@jgrund See #469

@jgrund
Copy link
Author

jgrund commented Oct 27, 2012

@sindresorhus #469 seems specific to pushState / rewriting a path to another one.

This proxy is intentionally generic so that it can provide any type of extra work that is needed for a specific route.

In my case I am using it to proxy to an image scaling / cropping service that will be on the same domain as my app in production, but not in development.

I think this will be useful as it will expose a hook point for any extra work needed to be done; in that way the functionality does not need to be baked into Yeoman and can be defined within the Gruntfile.

@jgrund
Copy link
Author

jgrund commented Oct 27, 2012

That would do what I need. Is there a timeline where grunt-contrib-server will supersede what is currently in Yeoman?

@sindresorhus
Copy link
Member

@jgrund Not yet, we need to settle on this one:
https://groups.google.com/d/topic/yeoman-dev/cMk1rBP4OA4/discussion
(Feel free to comment if you have any feedback)

@jgrund
Copy link
Author

jgrund commented Oct 28, 2012

@sindresorhus So, while I think the separation of concerns and greater control for the end user are both good things
I still need this functionality in place in Yeoman currently.

If either you or I can adapt your grunt-server pull request to be in the next version of Yeoman that would be helpful as I won't have to either maintain a branch or patch after every new release of Yeoman :)

@sindresorhus
Copy link
Member

@addyosmani This seems harmless and useful to some. I think we can merge it and just don't document it. ?

@thedjpetersen
Copy link

+1 This would be great

@fitzchak
Copy link

fitzchak commented Nov 6, 2012

This will be really awesome!
Will it be possible to rewrite any URL to the root, just if it doesn't start with the following URL: "$components/"?

@sindresorhus
Copy link
Member

I tried out this, but I couldn't get it to work, probably something I'm doing wrong, but bear with me:

Pulled down this PR and added this to the gruntfile:

proxies: {
      '^/api/v2/(.+)': function (req, res, next, match) {
        console.log('match',match);
        res.end('test');
      }
    }

Started the server and went to http://localhost:3501/api/v2/test, but just got 404 Error: /api/v2/test Not Found and no console.log output.

@jgrund
Copy link
Author

jgrund commented Nov 12, 2012

Just merged the latest from upstream master and this seems to be working for me (see screenshots):

Console output

Browser output

Can you try again and confirm it is still giving you a 404?

@sindresorhus
Copy link
Member

Thanks. Landed :)

@thedjpetersen
Copy link

@jpgrund Would you mind showing your example of forwarding requests? I am having a little trouble getting it working correctly.

@ro-ka
Copy link

ro-ka commented Nov 28, 2012

@jgrund @thedjpetersen I try to use this, but always get an 404, no matter what I do. Did this change again? How to integrate correctly?

@thedjpetersen
Copy link

@ro-ka how did you set up your proxy?

@thedjpetersen
Copy link

Look at the bottom of my post here http://www.thedjpetersen.com/blog/supercharging_yeoman/ to see how I set up my proxy. I also changed the server task to proxy POST data.

@ro-ka
Copy link

ro-ka commented Nov 28, 2012

@thedjpetersen I found your post and copied the code. :) Do you integrate this into grunt.initConfig() ? The request module I require at the top of the gruntfile.

@thedjpetersen
Copy link

@ro-ka can you post a gist of your code? (also if you copy pasted the code you may have to change the port for localhost)

@ro-ka
Copy link

ro-ka commented Nov 28, 2012

@thedjpetersen Uploaded a gist. The localhost url is the url of the api, right? I already have the problem that I get a 404 with this. console.log won't throw anything neither. My api is on another domain, so I have to call www.example.com with request.

@thedjpetersen
Copy link

Looks right to me - if you have your proxy set up the same way as mine, I am stumped.

@ro-ka
Copy link

ro-ka commented Nov 28, 2012

I have set it up the same – just using a different url for the api. 404 all the time. No fun. :(

@ro-ka
Copy link

ro-ka commented Nov 28, 2012

Found the reason: It's not landed yet. It's not in npm…

@sindresorhus When will this feature land in npm?

@thedjpetersen
Copy link

I should probably submit a patch to make this work with POST data too...

@sindresorhus
Copy link
Member

@ro-ka We only landed it because few people needed it, but it wasn't important enough to do a release.

We're doing some big changes in the coming weeks that will make this moot anyway.

@thedjpetersen We're going to use grunt-contrib-connect for the server part. I've added a middleware option there. You should use that for this purpose ;)

Let us know if you make anything reusable.

@ro-ka
Copy link

ro-ka commented Nov 29, 2012

@sindresorhus That's sad… Guess I will switch to Grunt when the new version is released, then. Is it planned to port this feature over to Grunt?

@sindresorhus
Copy link
Member

@ro-ka I'm sorry, I'm not sure what you mean?

@ro-ka
Copy link

ro-ka commented Nov 29, 2012

@sindresorhus This proxy feature could be written as a Grunt task, or? Would love to use that.

And since yeoman seems to change completely soon, atm it does not make sense to keep on using, imho.

@sindresorhus
Copy link
Member

This is just a middleware, yeoman will use grunt-contrib-connect which has an option to add middlewares. You will be able to just copy paste this into that option in your gruntfile or require it as a module.

That's why I suggested @thedjpetersen could make it reusable.

@ro-ka
Copy link

ro-ka commented Nov 30, 2012

@sindresorhus @thedjpetersen That would be great!

One thing that is important about the implementation is that the proxy needs to run on the same port (and domain of course) as the watch / server task. Else there are problems with CORS.

Is the grunt-contrib-connect module meant as a replacement for the watch task? Because you have to run it on your own… Isn't there a way to integrate that proxy feature in the 'normal' watch task?

@sindresorhus
Copy link
Member

The watch task only triggers other tasks when files changes. Currently it triggers the yeoman server task. Soon it will trigger the grunt-contrib-livereload task.

@ro-ka
Copy link

ro-ka commented Jan 16, 2013

@sindresorhus @thedjpetersen Is there some progress to release this as a grunt 0.4 plugin?

The proxy code that was in master got removed because of the changes made for yeoman 1.0.

@sindresorhus
Copy link
Member

@jgrund Would you be interested in making this into a generic connect middleware? Yeoman could then include/recommend it.

@thedjpetersen
Copy link

@sindresorhus I just stumbled across this the other day, I haven't tried it out yet though: https://github.com/tutukin/grunt-proxy . I am still patching yeoman to get proxy functionality.

@sindresorhus
Copy link
Member

@thedjpetersen It's just 3 lines of code. Only does hardcoded paths.

The reason I would like it as a connect middleware, is that yeoman 1.0 will come with grunt-contrib-connect which lets you add whatever middleware you want too it.

@jgrund
Copy link
Author

jgrund commented Jan 16, 2013

@sindresorhus, yes I'll spin up a repo for this shortly.

@soswow
Copy link

soswow commented Mar 2, 2013

Hey @sindresorhus here is middleware that does the thing https://gist.github.com/soswow/5072917 ;-)
Upd: Modified it with pipe() thx to @thedjpetersen

@thedjpetersen
Copy link

Here was my eventual workaround http://www.thedjpetersen.com/blog/proxying_grunt/

@drewzboto
Copy link

Thanks for all the examples. I also did a middleware proxy very similar to @thedjpetersen. I ended up using the http-proxy library as i needed some of the features there. I'm gonna tidy it up as a grunt-contrib plugin shortly, will post a link to the repo when it's ready.

@drewzboto
Copy link

So i put a quick grunt plugin together to provide proxying. My first attempt at a grunt plugin, any comments would be appreciated. Took a bit of inspiration from the LiveReload plugin, overall worked well with the middleware hook.

https://github.com/drewzboto/grunt-connect-proxy

@sindresorhus sindresorhus mentioned this pull request Mar 8, 2013
14 tasks
@blackjid
Copy link

Thanks @drewzboto it works very good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants