reactjs - How to enable proxy in react-slingshot (by coryhouse) so I can call my Django REST APIs? -


i have django development server running on port 8050 several rest apis serve react app, composed react-slingshot , running on localhost:3002.

what want javascript code, such fetch('api/v1/employees'), call localhost:8050 instead of localhost:3002.

i see raythree in github discussion able find solution question, however, unable solution work. implemented advice has it, code acts if made no changes @ all.

here code in tools/srcserver.js looks right now:

// file configures development web server // supports hot reloading , synchronized testing.  // require browsersync along webpack , middleware import browsersync 'browser-sync'; // required react-router browserhistory // see https://github.com/browsersync/browser-sync/issues/204#issuecomment-102623643 import historyapifallback 'connect-history-api-fallback'; import webpack 'webpack'; import webpackdevmiddleware 'webpack-dev-middleware'; import webpackhotmiddleware 'webpack-hot-middleware'; import config '../webpack.config.dev'; import proxy 'http-proxy-middleware';  const bundler = webpack(config);  const serverproxy = proxy('/api', {   target: "http://0.0.0.0:8050",   changeorigin: true, // set true hosted sites   loglevel: 'debug' });  // run browsersync , use middleware hot module replacement browsersync({   port: 3000,   ui: {     port: 3001   },    server: {     basedir: 'src',      middleware: [       historyapifallback(),        webpackdevmiddleware(bundler, {         // dev middleware can't access config, provide publicpath         publicpath: config.output.publicpath,          // these settings suppress noisy webpack output errors displayed console.         noinfo: true,         quiet: false,         stats: {           assets: false,           colors: true,           version: false,           hash: false,           timings: false,           chunks: false,           chunkmodules: false         }          // other settings see         // https://webpack.js.org/guides/development/#using-webpack-dev-middleware       }),        // bundler should same above       webpackhotmiddleware(bundler),        serverproxy,     ]   },    // no need watch '*.js' here, webpack take care of us,   // including full page reloads if hmr won't work   files: [     'src/*.html'   ] }); 

i figured out! apparently serverproxy needs indexed before webpackdevmiddleware , webpackhotmiddleware in middleware array!

// file configures development web server // supports hot reloading , synchronized testing.  // require browsersync along webpack , middleware import browsersync 'browser-sync'; // required react-router browserhistory // see https://github.com/browsersync/browser-sync/issues/204#issuecomment-102623643 import historyapifallback 'connect-history-api-fallback'; import webpack 'webpack'; import webpackdevmiddleware 'webpack-dev-middleware'; import webpackhotmiddleware 'webpack-hot-middleware'; import config '../webpack.config.dev'; import proxy 'http-proxy-middleware';  const bundler = webpack(config);  const serverproxy = proxy('/api', {   target: "http://0.0.0.0:8050",   changeorigin: true, // set true hosted sites   loglevel: 'debug' });  // run browsersync , use middleware hot module replacement browsersync({   port: 3000,   ui: {     port: 3001   },    server: {     basedir: 'src',      middleware: [       historyapifallback(),        // order of serverproxy important. not work if indexed       // after webpackdevmiddleware in array.       serverproxy,        webpackdevmiddleware(bundler, {         // dev middleware can't access config, provide publicpath         publicpath: config.output.publicpath,          // these settings suppress noisy webpack output errors displayed console.         noinfo: true,         quiet: false,         stats: {           assets: false,           colors: true,           version: false,           hash: false,           timings: false,           chunks: false,           chunkmodules: false         }          // other settings see         // https://webpack.js.org/guides/development/#using-webpack-dev-middleware       }),        // bundler should same above       webpackhotmiddleware(bundler),     ]   },    // no need watch '*.js' here, webpack take care of us,   // including full page reloads if hmr won't work   files: [     'src/*.html'   ] }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -