-
-
Save irfanandriansyah1997/a3ea6eab33c7f846f79f63a06f15fac9 to your computer and use it in GitHub Desktop.
Webpack dev server with a better proxy (http-proxy-middleware)
This file contains hidden or 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
| var express = require('express'); | |
| var path = require('path'); | |
| var webpackConfig = require('./webpack.config'); | |
| var webpack = require('webpack'); | |
| var webpackDevMiddleware = require('webpack-dev-middleware'); | |
| var webpackHotMiddleware = require('webpack-hot-middleware'); | |
| var proxyMiddleware = require('http-proxy-middleware'); | |
| var devConfig = webpackConfig.devServer; | |
| var app = express(); | |
| var compiler = webpack(webpackConfig); | |
| app.use(express.static(devConfig.contentBase || __dirname)); | |
| app.use(webpackDevMiddleware(compiler, {})); | |
| app.use(webpackHotMiddleware(compiler)); | |
| // Set up the proxy. | |
| if(devConfig.proxy) { | |
| Object.keys(devConfig.proxy).forEach(function(context) { | |
| app.use(proxyMiddleware(context, devConfig.proxy[context])); | |
| }); | |
| } | |
| if(devConfig.historyApiFallback) { | |
| console.log('404 responses will be forwarded to /index.html'); | |
| app.get('*', function(req, res) { | |
| res.sendFile(path.resolve(devConfig.contentBase, 'index.html')); | |
| }); | |
| } | |
| app.listen(devConfig.port || 8080, function() { | |
| console.log('Development server listening on port ' + devConfig.port); | |
| }); |
This file contains hidden or 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
| var webpack = require('webpack'); | |
| module.exports = { | |
| entry: [ | |
| ..., | |
| 'webpack-hot-middleware/client?reload=true' | |
| ], | |
| plugins: [ | |
| new webpack.optimize.OccurenceOrderPlugin(), | |
| new webpack.HotModuleReplacementPlugin(), | |
| new webpack.NoErrorsPlugin() | |
| ], | |
| devServer: { | |
| port: 8081, | |
| contentBase: 'src/', | |
| historyApiFallback: true, | |
| proxy: { | |
| '/api': { | |
| target: 'http://localhost:8080', | |
| pathRewrite: { | |
| '^/api' : '' | |
| } | |
| } | |
| } | |
| }, | |
| ... | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment