aldikhan13

CUSTOM PROXY IN WEBPACK DEV SERVER LIKE CRA

Sep 3rd, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // TIPS AND TRICK CUSTOM PROXY IN WEBPACK DEV SERVER BY restuwahyu13
  2. //NOTE this method like using CRA in React
  3.  
  4. // manual proxy before
  5. module.exports = {
  6.     devServer: {
  7.         open: true,
  8.         compress: true,
  9.         hot: true,
  10.         inline: true,
  11.         lazy: true,
  12.         contentBase: resolve(process.cwd(), 'build'),
  13.         port: process.env.PORT || 3000,
  14.         proxy: {
  15.           '/api/*', {
  16.             target: 'http://localhost:3001',
  17.             secure: false,
  18.             changeOrigin: true,
  19.             headers: {
  20.                 'Access-Control-Allow-Origin': '*',
  21.                 'Access-Control-Allow-Credentials': '*',
  22.                 'Access-Control-Allow-Methods': '*'
  23.             }
  24.           }
  25.         }
  26.         liveReload: false
  27.     }
  28. }
  29.  
  30. // custom proxy after
  31. module.exports = {
  32.     devServer: {
  33.         open: true,
  34.         compress: true,
  35.         hot: true,
  36.         inline: true,
  37.         lazy: true,
  38.         contentBase: resolve(process.cwd(), 'build'),
  39.         port: process.env.PORT || 3000,
  40.         before: (app, server, compiler) => {
  41.             const pathProxy = resolve(process.cwd(), 'src/setupProxy')
  42.             return require(`${pathProxy}`)(app)
  43.         },
  44.         liveReload: false
  45.     }
  46. }
Add Comment
Please, Sign In to add comment