在 Create React App Typescript 中未读取 SetupProxy 文件

jt2*_*t25 6 javascript typescript reactjs create-react-app

CRA --template typescriptsetupProxy.js文件中使用了这段代码

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {
  app.use(
    '/user/try',
    createProxyMiddleware({
      target: 'http://localhost:8002',
      changeOrigin: true,
    })
  );
};
Run Code Online (Sandbox Code Playgroud)

但问题是setupProxy.js文件被完全忽略导致我的请求失败。

我知道我可以使用"proxy": "http://localhost:8002"inpackage.json但它不接受多个代理。

为了进一步测试,我还创建了一个新的CRA没有打字稿,突然它可以使用上面相同的代码。

有没有办法setupProxy.jsCRA 打字稿中工作?

小智 1

应该能够将其添加到您的 tsconfig.json 中。希望对你有帮助。

"exclude": ["src/setupProxy.js"]
Run Code Online (Sandbox Code Playgroud)