【问题标题】:How to request mongodb atlas Database with Heroku如何使用 Heroku 请求 mongodb atlas 数据库
【发布时间】:2019-08-17 21:07:16
【问题描述】:

我需要部署一个应用程序。我使用 React JS、Node JS 和 Mongodb atlas 来做到这一点。我试图在 Heroku 上部署它,但是当我在线尝试时无法访问我的数据库(在本地开发人员上工作正常)。使用 Heroku 访问我的数据库的好的语法是什么?

网站链接:https://teachonline.herokuapp.com/

链接到我的github:https://github.com/romainbor/eteach

我尝试了很多不同的语法来访问我的数据库,但总是失败。

我将代码原样放在本地开发人员中。

export const register = (user_name, password, email, category, skills, description, dispatch) => {
 const data = {
     user_name, 
     password,
     description,
     email,
     category,
     skills
 };
 let myHeaders = new Headers();
 myHeaders.append("Content-type", "application/json");
 myHeaders.append("Authorization", "Bearer "+localStorage.getItem('tokenJWT'));
     fetch ('http://localhost:3001/user',
     {
         method:'POST',
         mode: "cors",
         headers : myHeaders,
         body: JSON.stringify(data)
     })
     .then(response => response.json())
     .catch(error => (error));



     return{
         type: "REQUEST_REGISTER",
         payload: {}
     }
}; 

我的客户端文件夹中的 package.json


"proxy": "http://localhost:5000"

如果我删除代理线路,我的应用程序在线但我永远无法访问我的数据库,因此我的应用程序无法使用。 如果我把代理行我得到“无效的主机头”

所以我误解了一些东西,但我不知道到底是什么

【问题讨论】:

    标签: node.js reactjs heroku web-deployment


    【解决方案1】:

    我发现有两件事我觉得不对。

    1) 您正在调用fetch ('http://localhost:3001/user',,您不知道您的应用程序将在生产服务器上运行的哪个端口,所以我想您应该只使用fetch ('/

    1) 在您的 index.js 中(在服务器目录中,我在您的 github 上找到)您在第 13 行定义 const PORT = 5000; 但这是同样的问题,因为您不知道生产端口,因此您可以更改this to const PORT = process.env.PORT || 5000; 这样它会在 env 变量中检查服务器的 PORT,如果找不到(例如在您的本地机器上),则仅使用 5000。

    希望这会有所帮助。

    【讨论】:

    • 您好,首先感谢您的回复。我试过你说的,但我的客户现在出错了:“端口 12345 上已经有东西在运行”。我误解了一些我认为...但我不知道到底是什么
    猜你喜欢
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2020-10-07
    相关资源
    最近更新 更多