Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Simplify proxy addon example #62

Merged
merged 2 commits into from
Mar 22, 2018

Conversation

alecmev
Copy link
Contributor

@alecmev alecmev commented Mar 16, 2018

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

Motivation / Use-Case

Updated the proxy addon example to be closer to what people probably use it for in the context of webpack-serve / webpack-dev-server. Routing usually isn't needed with http-proxy, and when it is, koa-mount would probably be a better fit.

Additional Info

I think a combined example of HTTP proxy + history API fallback is warranted, but I want to know whether you think it's needed or not first, before adding it.

Also, would be nice to have all examples koa-connect-free. I can see why they aren't right now, the existing Koa 2 equivalent libs are a bit sub par, but that's addressable.

@codecov
Copy link

codecov bot commented Mar 16, 2018

Codecov Report

Merging #62 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #62   +/-   ##
=======================================
  Coverage   92.19%   92.19%           
=======================================
  Files           7        7           
  Lines         269      269           
=======================================
  Hits          248      248           
  Misses         21       21

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a436807...d6fb2e3. Read the comment docs.

@shellscape
Copy link
Contributor

Thanks for the PR 🍺

I'm good with an additional example, but there's no practical reason to remove information here. The original example should stay. If you'd like to move you changes into proxy-config-simple.js or some such, I'd be happy to accept it.

@alecmev
Copy link
Contributor Author

alecmev commented Mar 16, 2018

What confuses me the most, about the original example, is why have a router at all? The proxy middleware does its own URL checks, what does the router do?

And, related to that, why it "must be the last middleware added"? The app can function fine while having the proxy as the first middleware in the chain, as long as it isn't catch-all.

I think the main proxy example should be about proxying /api (no stats, but I have a hunch that it covers 99% of use cases), and then a proxy-advanced should be about explaining what would happen if the proxy were to be used without a context, and showing the role of middleware.webpack and middleware.content. And then maybe a yet another example with a koa-mount / koa-router?

@shellscape
Copy link
Contributor

Please see my previous comment; That's what I'll accept in a PR at this time.

};

// This add-on will route all incoming requests for
// "http://localhost:8080/api/*" to "http://localhost:8081/api/*" (note the port
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it may be something other than 8080, but decided to go for brevity.

module.exports.serve = {
content: [__dirname],
add: (app, middleware, options) => {
app.use(convert(proxy('/api', { target: 'http://localhost:8081' })));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline options, to make it easier to copy-paste.

@@ -386,6 +386,7 @@ Listed below are some of the add-on patterns and recipes that can be found in
- [compress](docs/addons/compress)
- [historyApiFallback](docs/addons/history-fallback.config.js)
- [proxy](docs/addons/proxy.config.js)
- [spa-with-api-simple](docs/addons/spa-with-api-simple.config.js)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is hard, but seems self-explanatory and catchy enough.

// applications.
//
// To remove the "/api" prefix when proxying the API requests, just add
// "pathRewrite: { '^/api': '' }" to proxy's options.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is common enough to be worth a protip.

@shellscape
Copy link
Contributor

Cheers 🍺

@shellscape shellscape merged commit 67c1050 into webpack-contrib:master Mar 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants