在 heroku 上部署了 React + redux 应用程序,但 backend/api 不工作
React + redux app deployed on heroku but backend/api not working
这是我的应用程序 link https://course-s.herokuapp.com/ and here is my Github repo https://github.com/shaikafroz016/Course-S。该应用程序在 localhost whit express、MongoDB Atlas 和作为后端的节点服务器以及作为前端的 react-redux 中完美运行,但是当我尝试在 Heroku 上 运行 这个时,我说的是无法获取什么问题。
编辑:
我在我的客户中提到了基础 url 作为 localhost:3000 所以我是否应该将 heroku app link 添加到基础 url?你能帮我做什么吗?而且当我尝试使用后端本地主机 (localhost:3000) 的应用程序时,它工作正常,因为我关闭了本地服务器,heroku 应用程序说 faild to fetch
这一定是因为 CORS (Access-Control-Allow-Origin),这是一个解决方法:
运行 npm install cors
然后将其添加到您的 app.js 文件中:
var cors = require('cors')
app.use(cors())
请务必在所有请求定义之前添加此内容(app.get、app.post 等...)
现已启用 CORS。
Access-Control-Allow-Origin header 确定允许哪些来源通过 CORS 访问服务器资源。
解释:https://medium.com/@alexishevia/using-cors-in-express-cac7e29b005b
这是我的应用程序 link https://course-s.herokuapp.com/ and here is my Github repo https://github.com/shaikafroz016/Course-S。该应用程序在 localhost whit express、MongoDB Atlas 和作为后端的节点服务器以及作为前端的 react-redux 中完美运行,但是当我尝试在 Heroku 上 运行 这个时,我说的是无法获取什么问题。 编辑: 我在我的客户中提到了基础 url 作为 localhost:3000 所以我是否应该将 heroku app link 添加到基础 url?你能帮我做什么吗?而且当我尝试使用后端本地主机 (localhost:3000) 的应用程序时,它工作正常,因为我关闭了本地服务器,heroku 应用程序说 faild to fetch
这一定是因为 CORS (Access-Control-Allow-Origin),这是一个解决方法:
运行 npm install cors
然后将其添加到您的 app.js 文件中:
var cors = require('cors')
app.use(cors())
请务必在所有请求定义之前添加此内容(app.get、app.post 等...)
现已启用 CORS。
Access-Control-Allow-Origin header 确定允许哪些来源通过 CORS 访问服务器资源。
解释:https://medium.com/@alexishevia/using-cors-in-express-cac7e29b005b