Skip to content

Commit

Permalink
feat: add proxy with http-proxy-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven committed Sep 7, 2017
1 parent 2aa5557 commit 9a49cce
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"file-loader": "^0.11.1",
"fs-extra": "^4.0.1",
"html-webpack-plugin": "^2.24.1",
"http-proxy-middleware": "^0.17.4",
"node-sass": "^4.5.3",
"rimraf": "^2.6.1",
"sass-loader": "^6.0.5",
Expand Down
3 changes: 2 additions & 1 deletion project.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
'redux-thunk',
'react-router-dom',
'reactstrap',
'react-spinkit'
'react-spinkit',
'prop-types'
],
}
7 changes: 7 additions & 0 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const logger = require('../build/lib/logger')
const webpackConfig = require('../build/webpack.config')
const project = require('../project.config')
const compress = require('compression')
const proxy = require('http-proxy-middleware')

const app = express()
app.use(compress())
Expand Down Expand Up @@ -36,6 +37,12 @@ if (project.env === 'development') {
// 设置静态资源路径,浏览器会默认请求该路径下favicon.ico
app.use(express.static(path.resolve(project.basePath, 'public')))

// Proxy api requests
app.use('/zen', proxy({
target: 'https://api.github.com',
changeOrigin: true
}))

// 重定向到index.html
app.use('*', function (req, res, next) {
const filename = path.join(compiler.outputPath, 'index.html')
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/PageLayout/PageLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class PageLayout extends React.Component {
<Collapse isOpen={this.state.isOpen} navbar>
<Nav className="ml-auto" navbar>
<NavLink to="/counter" activeClassName="active" tag={Link}>Counter</NavLink>
<NavLink to="/zen" activeClassName="active" tag={Link}>Zen</NavLink>
<NavLink to="/zenPage" activeClassName="active" tag={Link}>Zen</NavLink>
<NavLink to="/elapse" activeClassName="active" tag={Link}>Elapse</NavLink>
<NavLink to={location} activeClassName="active" tag={Link}>Route</NavLink>
<NavLink to="/pageNotFound" activeClassName="active" tag={Link}>404</NavLink>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Zen/modules/zen.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function fetchZen() {
if (getState().zen.fetching) return

dispatch(requestZen())
axios.get('https://api.github.com/zen')
axios.get('/zen')
.then((res) => {
console.log(res)
dispatch(receiveZen(res.data))
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Routes = () => (
<Switch>
<Route exact path="/" component={Home} />
<Route path="/counter" component={AsyncCounter} />
<Route path="/zen" component={AsyncZen} />
<Route path="/zenPage" component={AsyncZen} />
<Route path="/elapse" component={AsyncElapse} />
<Route path="/route/:id" component={AsyncRoute} />
<Route path="/404" component={AsyncPageNotFound} />
Expand Down
38 changes: 36 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,10 @@ event-emitter@~0.3.5:
d "1"
es5-ext "~0.10.14"

eventemitter3@1.x.x:
version "1.2.0"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-1.2.0.tgz#1c86991d816ad1e504750e73874224ecf3bec508"

events@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
Expand Down Expand Up @@ -2570,6 +2574,22 @@ http-errors@~1.6.2:
setprototypeof "1.0.3"
statuses ">= 1.3.1 < 2"

http-proxy-middleware@^0.17.4:
version "0.17.4"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-0.17.4.tgz#642e8848851d66f09d4f124912846dbaeb41b833"
dependencies:
http-proxy "^1.16.2"
is-glob "^3.1.0"
lodash "^4.17.2"
micromatch "^2.3.11"

http-proxy@^1.16.2:
version "1.16.2"
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.16.2.tgz#06dff292952bf64dbe8471fa9df73066d4f37742"
dependencies:
eventemitter3 "1.x.x"
requires-port "1.x.x"

http-signature@~1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.1.1.tgz#df72e267066cd0ac67fb76adf8e134a8fbcf91bf"
Expand Down Expand Up @@ -2732,6 +2752,10 @@ is-extglob@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-1.0.0.tgz#ac468177c4943405a092fc8f29760c6ffc6206c0"

is-extglob@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"

is-finite@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-finite/-/is-finite-1.0.2.tgz#cc6677695602be550ef11e8b4aa6305342b6d0aa"
Expand All @@ -2754,6 +2778,12 @@ is-glob@^2.0.0, is-glob@^2.0.1:
dependencies:
is-extglob "^1.0.0"

is-glob@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-3.1.0.tgz#7ba5ae24217804ac70707b96922567486cc3e84a"
dependencies:
is-extglob "^2.1.0"

is-number@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-number/-/is-number-2.1.0.tgz#01fcbbb393463a548f2f466cce16dece49db908f"
Expand Down Expand Up @@ -3106,7 +3136,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@~4.17.4:
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@~4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

Expand Down Expand Up @@ -3196,7 +3226,7 @@ methods@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"

micromatch@^2.1.5:
micromatch@^2.1.5, micromatch@^2.3.11:
version "2.3.11"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"
dependencies:
Expand Down Expand Up @@ -4423,6 +4453,10 @@ require-uncached@^1.0.3:
caller-path "^0.1.0"
resolve-from "^1.0.0"

requires-port@1.x.x:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"

resolve-from@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226"
Expand Down

0 comments on commit 9a49cce

Please sign in to comment.