简体   繁体   English

使用 Typescript 创建 React App 中的自定义代理

[英]Custom Proxy in Create React App using Typescript

I need to proxy requests from a Create React App to a separate API server, and set that server dynamically or with environment variables.我需要将来自 Create React App 的请求代理到单独的 API 服务器,并动态设置该服务器或使用环境变量。 I followed configuring proxy manually , however I am using TypeScript.我按照手动配置代理,但是我使用的是 TypeScript。 react-scripts-ts does not seem to load src/setupProxy.js even after updating to latest version (v3.1.0).即使更新到最新版本(v3.1.0), react-scripts-ts似乎也不会加载src/setupProxy.js I got it working with vanilla javascript, but am unable to get it to work with TypeScript.我让它与 vanilla javascript 一起工作,但我无法让它与 TypeScript 一起工作。 Has anyone gotten setupProxy to work with React TypeScript?有没有人让 setupProxy 与 React TypeScript 一起工作?

Now CRA supports Typescript but I couldn't make setupProxy.js to work.现在 CRA 支持 Typescript 但我无法让setupProxy.js工作。

My mistake was super dumb.我的错误是超级愚蠢的。 setupProxy was outside src/ folder. setupProxysrc/文件夹之外。 So, make sure that you create setupProxy inside the folder src因此,请确保在文件夹src创建setupProxy

src/setupProxy.js

My code looks like this:我的代码如下所示:

module.exports = function (app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: process.env.REACT_APP_API_URI,
      changeOrigin: true,
    })
  )
}

Also, make sure that your env configuration is working.另外,请确保您的env配置正常工作。 You need to install the package env-cmd and replace您需要安装包env-cmd并替换

"start": "react-scripts start",

for为了

"start": "env-cmd -f .env.development.local react-scripts start",

After code diving, it appears the typescript create-react-app has not yet incorporated custom proxy functionality.代码潜水后,似乎打字稿 create-react-app尚未合并自定义代理功能。 I had to update two files:我不得不更新两个文件:

https://github.com/samuelstevens9/create-react-app/blob/next/packages/react-scripts/config/paths.js https://github.com/samuelstevens9/create-react-app/blob/next/packages/react-scripts/config/paths.js

Added proxySetup: resolveApp('src/setupProxy.js'), to each module.exports, the last (3rd) being proxySetup: resolveOwn('template/src/setupProxy.js'),添加proxySetup: resolveApp('src/setupProxy.js'),到每个module.exports,最后(第三个)是proxySetup: resolveOwn('template/src/setupProxy.js'),

https://github.com/samuelstevens9/create-react-app/blob/next/packages/react-scripts/config/webpackDevServer.config.js https://github.com/samuelstevens9/create-react-app/blob/next/packages/react-scripts/config/webpackDevServer.config.js

Added const fs = require('fs');添加const fs = require('fs'); below line 15 const paths = require('./paths');在第 15 行下面const paths = require('./paths'); and added并添加

if (fs.existsSync(paths.proxySetup)) {
    // This registers user provided middleware for proxy reasons
    require(paths.proxySetup)(app);
}

inside the before(app) { ... } function towards the end of the file.在文件末尾的before(app) { ... }函数中。

I am working on creating a pull request to the main repo, but it looks like the v3.1.0 files are different from the most up to date ones on the next branch.我正在向主存储库创建拉取请求,但看起来 v3.1.0 文件与next分支上的最新文件不同。 For now I use a patch script I made since we are using a lerna monorepo that updates all necessary packages:现在我使用我制作的补丁脚本,因为我们使用的是更新所有必要包的 lerna monorepo:

#!/bin/bash
CONFIG_PATHS_URL="https://raw.githubusercontent.com/samuelstevens9/create-react-app/next/packages/react-scripts/config/paths.js"
CONFIG_WEBPACKDEVSERVER_URL="https://raw.githubusercontent.com/samuelstevens9/create-react-app/next/packages/react-scripts/config/webpackDevServer.config.js"
SETUPPROXY_URL="https://gist.githubusercontent.com/samuelstevens9/5872e72ac915dfc1a8ae2fdcef323899/raw/7f2c76d42bc0915026379dfc7884cb1bd97f56bb/setupProxy.js"

for f in packages/*; do
    if [ -d ${f} ]; then
        echo $f
        # Will not run if no directories are available
        NODE_MODULES_CONFIG_DIR=$f/node_modules/react-scripts-ts/config
        if [ -d "$NODE_MODULES_CONFIG_DIR" ]; then
          # Control will enter here if $DIRECTORY exists.
          echo $NODE_MODULES_CONFIG_DIR
          curl -o $NODE_MODULES_CONFIG_DIR/paths.js $CONFIG_PATHS_URL
          curl -o $NODE_MODULES_CONFIG_DIR/webpackDevServer.config.js $CONFIG_WEBPACKDEVSERVER_URL
          curl -o $f/src/setupProxy.js $SETUPPROXY_URL
        fi
    fi
done

And updates the setupProxy.js file in each package as well.并更新每个包中的setupProxy.js文件。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM