-
-
Notifications
You must be signed in to change notification settings - Fork 734
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
Conversation
- 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 #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. |
That would do what I need. Is there a timeline where grunt-contrib-server will supersede what is currently in Yeoman? |
@jgrund Not yet, we need to settle on this one: |
@sindresorhus So, while I think the separation of concerns and greater control for the end user are both good things 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 :) |
@addyosmani This seems harmless and useful to some. I think we can merge it and just don't document it. ? |
+1 This would be great |
This will be really awesome! |
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 |
Thanks. Landed :) |
@jpgrund Would you mind showing your example of forwarding requests? I am having a little trouble getting it working correctly. |
@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? |
@ro-ka how did you set up your proxy? |
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. |
@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. |
@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) |
@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. |
Looks right to me - if you have your proxy set up the same way as mine, I am stumped. |
I have set it up the same – just using a different url for the api. 404 all the time. No fun. :( |
Found the reason: It's not landed yet. It's not in npm… @sindresorhus When will this feature land in npm? |
I should probably submit a patch to make this work with POST data too... |
@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. |
@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? |
@ro-ka I'm sorry, I'm not sure what you mean? |
@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. |
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. |
@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? |
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. |
@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. |
@jgrund Would you be interested in making this into a generic connect middleware? Yeoman could then include/recommend it. |
@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. |
@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. |
@sindresorhus, yes I'll spin up a repo for this shortly. |
Hey @sindresorhus here is middleware that does the thing https://gist.github.com/soswow/5072917 ;-) |
Here was my eventual workaround http://www.thedjpetersen.com/blog/proxying_grunt/ |
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. |
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. |
Thanks @drewzboto it works very good! |
In
Gruntfile.js
you can add a block like this: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.