This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Simplify proxy addon example #62
Merged
shellscape
merged 2 commits into
webpack-contrib:master
from
alecmev:simplify-proxy-example
Mar 22, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.