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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

- [staticOptions](docs/addons/static-content-options.config.js)
- [useLocalIp](docs/addons/local-ip.config.js)

Expand Down
37 changes: 37 additions & 0 deletions docs/addons/spa-with-api-simple.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const path = require('path');

const history = require('connect-history-api-fallback');
const proxy = require('http-proxy-middleware');
const convert = require('koa-connect');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
},
mode: 'development',
output: {
filename: 'output.js'
}
};

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.

app.use(convert(history()));
}
};

// 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.

// change), and all remaining requests, which would otherwise result in 404,
// will be rewritten to serve your "index.html", which is useful for single-page
// 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.

//
// Proxy's docs: https://github.com/chimurai/http-proxy-middleware
// Fallback's docs: https://github.com/bripkens/connect-history-api-fallback