首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react-slingshot (由coryhouse)中启用代理,以便我可以调用我的Django REST API?

如何在react-slingshot (由coryhouse)中启用代理,以便我可以调用我的Django REST API?
EN

Stack Overflow用户
提问于 2017-09-12 07:43:01
回答 1查看 208关注 0票数 2

我有一个运行在端口8050上的Django开发服务器,其中有几个REST,我希望为我的React应用程序提供服务,该应用程序由react-slingshot组成,在localhost:3002上运行。

我想要的是让我的Javascript代码,比如fetch('api/v1/employees'),实际调用localhost:8050而不是localhost:3002。

我看到raythree in a github discussion能够找到我的问题的解决方案,但是,我无法让他的解决方案工作。我完全按照他的方式实现了他的建议,但代码表现得好像我根本没有做任何更改。

下面是我在tools/srcServer.js中的代码现在的样子:

代码语言:javascript
复制
// This file configures the development web server
// which supports hot reloading and synchronized testing.

// Require Browsersync along with webpack and middleware for it
import browserSync from 'browser-sync';
// Required for react-router browserHistory
// see https://github.com/BrowserSync/browser-sync/issues/204#issuecomment-102623643
import historyApiFallback from 'connect-history-api-fallback';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from '../webpack.config.dev';
import proxy from 'http-proxy-middleware';

const bundler = webpack(config);

const serverProxy = proxy('/api', {
  target: "http://0.0.0.0:8050",
  changeOrigin: true, // set true for hosted sites
  logLevel: 'debug'
});

// Run Browsersync and use middleware for Hot Module Replacement
browserSync({
  port: 3000,
  ui: {
    port: 3001
  },

  server: {
    baseDir: 'src',

    middleware: [
      historyApiFallback(),

      webpackDevMiddleware(bundler, {
        // Dev middleware can't access config, so we provide publicPath
        publicPath: config.output.publicPath,

        // These settings suppress noisy webpack output so only errors are displayed to the console.
        noInfo: true,
        quiet: false,
        stats: {
          assets: false,
          colors: true,
          version: false,
          hash: false,
          timings: false,
          chunks: false,
          chunkModules: false
        }

        // for other settings see
        // https://webpack.js.org/guides/development/#using-webpack-dev-middleware
      }),

      // bundler should be the same as above
      webpackHotMiddleware(bundler),

      serverProxy,
    ]
  },

  // no need to watch '*.js' here, webpack will take care of it for us,
  // including full page reloads if HMR won't work
  files: [
    'src/*.html'
  ]
});
EN

回答 1

Stack Overflow用户

发布于 2017-09-12 07:43:01

我想通了!显然,在数组中,serverProxy需要在 webpackDevMiddlewarewebpackHotMiddleware之前被索引为middleware

代码语言:javascript
复制
// This file configures the development web server
// which supports hot reloading and synchronized testing.

// Require Browsersync along with webpack and middleware for it
import browserSync from 'browser-sync';
// Required for react-router browserHistory
// see https://github.com/BrowserSync/browser-sync/issues/204#issuecomment-102623643
import historyApiFallback from 'connect-history-api-fallback';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from '../webpack.config.dev';
import proxy from 'http-proxy-middleware';

const bundler = webpack(config);

const serverProxy = proxy('/api', {
  target: "http://0.0.0.0:8050",
  changeOrigin: true, // set true for hosted sites
  logLevel: 'debug'
});

// Run Browsersync and use middleware for Hot Module Replacement
browserSync({
  port: 3000,
  ui: {
    port: 3001
  },

  server: {
    baseDir: 'src',

    middleware: [
      historyApiFallback(),

      // The order of serverProxy is important. It will not work if it is indexed
      // after the webpackDevMiddleware in this array.
      serverProxy,

      webpackDevMiddleware(bundler, {
        // Dev middleware can't access config, so we provide publicPath
        publicPath: config.output.publicPath,

        // These settings suppress noisy webpack output so only errors are displayed to the console.
        noInfo: true,
        quiet: false,
        stats: {
          assets: false,
          colors: true,
          version: false,
          hash: false,
          timings: false,
          chunks: false,
          chunkModules: false
        }

        // for other settings see
        // https://webpack.js.org/guides/development/#using-webpack-dev-middleware
      }),

      // bundler should be the same as above
      webpackHotMiddleware(bundler),
    ]
  },

  // no need to watch '*.js' here, webpack will take care of it for us,
  // including full page reloads if HMR won't work
  files: [
    'src/*.html'
  ]
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46165800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档