简体   繁体   English

通过 env 将 Node.js 连接到 React.js 的问题

[英]Problems with connection Node.js to React.js via env

I have created my fullstack (Node.js and React.js) blog app and now when I want to deploy my frontend and of course backend, I have noticed, that I don't know how to do it in a proper way.我已经创建了我的全栈(Node.js 和 React.js)博客应用程序,现在当我想部署我的前端,当然还有后端时,我注意到,我不知道如何以正确的方式去做。

My application works fine when I use localhost but if I deploy it, my links won't be like "localhost:8080/api/blablabla", but for example some heroku slug (url) and my Node.js routes.当我使用 localhost 时,我的应用程序工作正常,但如果我部署它,我的链接不会像“localhost:8080/api/blablabla”,而是例如一些 heroku slug (url) 和我的 Node.js 路由。

In my Node.js it looks like this:在我的 Node.js 中,它看起来像这样:

在此处输入图像描述

And here below my app.js code:在我的 app.js 代码下面:

在此处输入图像描述

And from React.js (frontend side) it looks like this:从 React.js(前端)看起来像这样:

在此处输入图像描述

So, the question is what I need to add.所以,问题是我需要补充什么。 I suppose, that on React (frontend) must me something like env.我想,在 React(前端)上,我必须像 env 一样。 variables with backend link?带有后端链接的变量? And before it, better to add backend to know what the slug (url) would be, because it is random.在此之前,最好添加后端以了解 slug (url) 将是什么,因为它是随机的。 But I dont know do I need to add something on backend or not.但我不知道我是否需要在后端添加一些东西。

If I wrote something wrong, I will be really appreciate if you correct me:)如果我写错了,如果你纠正我,我将非常感激:)

ThanK you in advance先感谢您

In the last React.js image you have shared, I can see you are using axios to send the GET request.在您分享的最后一个 React.js 图像中,我可以看到您正在使用axios发送 GET 请求。 However, you are providing a url which is a localhost one.但是,您提供的是localhost url。 So I assume you are trying to make a request to the backend that you have hosted on Heroku, but instead, the url is pointing to your local environment.因此,我假设您正在尝试向您在 Heroku 上托管的后端发出请求,但是 url 指向您的本地环境。 What I would recommend doing is changing the http://localhost:5000 part to the Heroku app's url.我建议将http://localhost:5000部分更改为 Heroku 应用程序的 url。

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

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