Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 部署到Heroku时,如何在React app中设置代理而不创建React app_Reactjs_Heroku_Proxy - Fatal编程技术网

Reactjs 部署到Heroku时,如何在React app中设置代理而不创建React app

Reactjs 部署到Heroku时,如何在React app中设置代理而不创建React app,reactjs,heroku,proxy,Reactjs,Heroku,Proxy,我基于我的模板()创建了一个React应用程序。现在我正试图将它部署到Heroku,并连接到ExpressRESTAPI。我想知道,如何设置代理,以便所有对/api/*的请求都有一个我后端的来源。我已经尝试使用设置static.json文件 "proxies": { "/api" : { "origin": "https://${BACKEND_URL}/api" } } 但它似乎不起作用,因为我不断地收到405个请求,因为有人向前端的/api发出请求 index_bundl

我基于我的模板()创建了一个React应用程序。现在我正试图将它部署到Heroku,并连接到ExpressRESTAPI。我想知道,如何设置代理,以便所有对/api/*的请求都有一个我后端的来源。我已经尝试使用设置static.json文件

"proxies": {
  "/api" : {
    "origin": "https://${BACKEND_URL}/api"
  }
}

但它似乎不起作用,因为我不断地收到405个请求,因为有人向前端的/api发出请求

index_bundle.js?bace5e170912d902f943:12 POST https://${FRONTEND_URL}/api/auth/signup 405 (Method Not Allowed)
没有使用create-react应用程序,还有其他方法可以做到这一点吗

static.json

{
  "root": "dist/",
  "routes": {
    "/**":"index.html"
  },
  "proxies": {
    "/api": {
      "origin": "https://${BACKEND_URL}/api"
    }
  }
}
{
  "name": "warbler-client",
  "version": "1.0.0",
  "main": "./src/index.jsx",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node_modules/.bin/webpack-dev-server --mode development --hot",
    "build": "node_modules/.bin/webpack --config webpack.prod.js --mode production",
    "start": "node_modules/.bin/http-server ./dist"
  },
  "license": "MIT",
  "dependencies": {
    "http-server": "^0.12.1",
    "jwt-decode": "^2.2.0",
    "ky": "^0.19.0",
    "moment": "^2.24.0",
    "normalize.css": "^8.0.1",
    "prop-types": "^15.7.2",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "react-moment": "^0.9.7",
    "react-redux": "^7.2.0",
    "react-router-dom": "^5.1.2",
    "react-router-prop-types": "^1.0.4",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^0.88.2"
  },
  "devDependencies": {
    "@babel/core": "^7.8.7",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-transform-runtime": "^7.8.3",
    "@babel/preset-env": "^7.8.7",
    "@babel/preset-react": "^7.8.3",
    "@babel/runtime": "^7.8.7",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.0.6",
    "css-loader": "^3.4.2",
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "1.7.0",
    "favicons-webpack-plugin": "^3.0.1",
    "file-loader": "^5.1.0",
    "html-webpack-plugin": "^4.0.0-beta.11",
    "js-beautify": "^1.10.3",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.13.1",
    "prettier": "^1.19.1",
    "sass-loader": "^8.0.2",
    "webpack": "^4.42.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  },
}

package.json

{
  "root": "dist/",
  "routes": {
    "/**":"index.html"
  },
  "proxies": {
    "/api": {
      "origin": "https://${BACKEND_URL}/api"
    }
  }
}
{
  "name": "warbler-client",
  "version": "1.0.0",
  "main": "./src/index.jsx",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node_modules/.bin/webpack-dev-server --mode development --hot",
    "build": "node_modules/.bin/webpack --config webpack.prod.js --mode production",
    "start": "node_modules/.bin/http-server ./dist"
  },
  "license": "MIT",
  "dependencies": {
    "http-server": "^0.12.1",
    "jwt-decode": "^2.2.0",
    "ky": "^0.19.0",
    "moment": "^2.24.0",
    "normalize.css": "^8.0.1",
    "prop-types": "^15.7.2",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "react-moment": "^0.9.7",
    "react-redux": "^7.2.0",
    "react-router-dom": "^5.1.2",
    "react-router-prop-types": "^1.0.4",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^0.88.2"
  },
  "devDependencies": {
    "@babel/core": "^7.8.7",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-transform-runtime": "^7.8.3",
    "@babel/preset-env": "^7.8.7",
    "@babel/preset-react": "^7.8.3",
    "@babel/runtime": "^7.8.7",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.0.6",
    "css-loader": "^3.4.2",
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "1.7.0",
    "favicons-webpack-plugin": "^3.0.1",
    "file-loader": "^5.1.0",
    "html-webpack-plugin": "^4.0.0-beta.11",
    "js-beautify": "^1.10.3",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.13.1",
    "prettier": "^1.19.1",
    "sass-loader": "^8.0.2",
    "webpack": "^4.42.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  },
}

我明白了

事情是,在默认情况下,Heroku检测到Node.js应用程序,成功地构建了它,并通过我的启动脚本提供了服务。所以它没有使用static.json。为了解决这个问题,只需在Node.js之后添加一个Heroku上的静态网站构建包。我希望这能帮助别人

找到了答案


事情是,在默认情况下,Heroku检测到Node.js应用程序,成功地构建了它,并通过我的启动脚本提供了服务。所以它没有使用static.json。为了解决这个问题,只需在Node.js之后添加一个Heroku上的静态网站构建包。我希望这能帮助别人

您想为http服务器/webpack/webpack开发服务器添加代理配置。对于http服务器,您可以使用-P标志将所有未解析的请求路由到端点。对于其他服务器,我不太熟悉,但我确信它们具有反向代理。检查文档。您将从CRA获得的代理仅用于开发目的。express是否同时为api端点和react应用程序静态构建文件提供服务?如果没有,为什么不将react请求指向绝对url?当mode/env为dev时,您可以指向/api;当不在mode/env dev时,您可以指向实际的绝对url。这是为了解决CORS问题吗?您想为http服务器/webpack/webpack dev服务器添加代理配置。对于http服务器,您可以使用-P标志将所有未解析的请求路由到端点。对于其他服务器,我不太熟悉,但我确信它们具有反向代理。检查文档。您将从CRA获得的代理仅用于开发目的。express是否同时为api端点和react应用程序静态构建文件提供服务?如果没有,为什么不将react请求指向绝对url?当mode/env为dev时,可以指向/api,当不在mode/env dev时,可以指向实际的绝对url。这是为了解决CORS问题吗?