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

Commit

Permalink
Simplify proxy addon example (#62)
Browse files Browse the repository at this point in the history
* Simplify proxy addon example

* Undo proxy.config.js changes, add spa-with-api-simple.config.js
  • Loading branch information
alecmev authored and shellscape committed Mar 22, 2018
1 parent 43205c2 commit 67c1050
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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)
- [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' })));
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
// 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.
//
// Proxy's docs: https://github.com/chimurai/http-proxy-middleware
// Fallback's docs: https://github.com/bripkens/connect-history-api-fallback

0 comments on commit 67c1050

Please sign in to comment.