简体   繁体   English

我的 React 前端无法调用我的 node-express 后端服务器,全栈应用程序部署在 heroku

[英]My React front-end is unable to call my node-express backend server, the fullstack app is deployed in heroku

const port = process.env.PORT || ("http://localhost:3002")
const postUrl=`${port}/post`;
addData=()=>{
    console.log(postUrl)
    console.log(process.env,"AS")
    Axios.post(postUrl,this.state.form)
    .then((response)=>{
    this.setState({errorMessage:"",successMessage:response.data})})
    .catch((err)=>{      
      this.setState({successMessage:"",errorMessage:err.response.data.message})
    })
  }

When I am calling the backend in the production process.env.PORT is blank.当我在生产过程中调用后端时,env.PORT 是空白的。

(process.env.NODE_EV= production which is absolutely correct exactly like in the backend) (process.env.NODE_EV=production,完全正确,就像后端一样)

My backend is completely fine as it getting the process.env.PORT correctly.我的后端完全没问题,因为它正确获取了 process.env.PORT。 But my frontend is not getting the process.env.PORT that's why it keeps calling the other address("http://localhost:3002").但是我的前端没有得到 process.env.PORT 这就是为什么它一直调用另一个地址(“http://localhost:3002”)。

App will completely work fine if I keep open my local machine backend because the "http://localhost:3002" is available to serve.如果我保持打开本地计算机后端,应用程序将完全正常工作,因为“http://localhost:3002”可以提供服务。 But in production, Heroku keeps changing the process.env.PORT which is showing its value in the backend, not in the frontend但在生产中,Heroku 不断更改 process.env.PORT,它在后端显示其价值,而不是在前端

How can I make my frontend to call my backend server properly in production??如何让我的前端在生产中正确调用我的后端服务器?

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const port = process.env.PORT || 3002;

const cors = require("cors");
const path=require("path");
const routing  = require("./routing/route");
require('dotenv').config();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use("/",routing);

app.use(function (err, req, res, next) {
  console.log("hii")
  res.status(500).send({ message: err.message });
});

if(process.env.NODE_ENV ==="production"){

  app.use(express.static("client/build"));

  app.get("*",(req,res)=>{
       res.sendFile(path.resolve((__dirname,"client","build","index.html")));
  });
}

app.listen(port, () => {
  console.log(` Server is started at http://localhost:${port}`);
});

server file服务器文件

If your React application is served from your Node.JS application as you said, you could just usewindow.location .如果如您所说,您的 React 应用程序是从您的 Node.JS 应用程序提供的,您可以只使用window.location window.location is an object that stores statistics about the current page that the user is on, and you could use that to construct a URL and send the server a request, like so: window.location是一个 object 存储有关用户当前页面的统计信息,您可以使用它来构造 URL 并向服务器发送请求,例如:

// This URL uses a template literal, which is a new feature of ES6.
// All but Internet Explorer supports it.
// This is using window.location.protocol, which is either `http:` or `https:`,
// depending on the protocol that the page was loaded with. window.location.host
// is the host that the page was loaded from, with the port number.
const postUrl =
  `${window.location.protocol}//${window.location.host}/post`;

// And then requesting with the URL.
addData = () => {
  console.log(postUrl);
  console.log(process.env, "AS");
  Axios.post(postUrl, this.state.form)
    .then((response) => {
      this.setState({errorMessage: "",successMessage: response.data});
    })
    .catch((err) => {      
      this.setState({successMessage: "",errorMessage: err.response.data.message});
    });
}

暂无
暂无

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

相关问题 React 全栈架构:在 node/express 应用中添加 react 前端时,react 的状态一般处理哪些方面? - React fullstack architecture: When adding a react front-end to a node/express application, what aspects does react's state generally handle? 如何将 Heroku 生成的服务器端口应用到我的前端 Vue.js 应用程序? - How can I apply the Heroku generated server port to my front-end Vue.js app? 我是否应该在我的前端网站代码(通过 Firebase 托管)中包含我的 NodeJS/Express 服务器代码(托管在 Heroku 上)? - Should I include my NodeJS/Express server code (hosted on Heroku) with my front-end website code (hosted through Firebase)? 如何将新的 React JS 前端嵌入到我现有的单体 Node JS 应用程序中? - How can I embed new React JS Front-end in my existing monolithic Node JS App? 使用 React 前端设置 Express 后端 - Setting up a Express backend with React Front-end 如何将后端(Node.js)链接到前端(React) - How to LInk Backend(Node.js) to front-End(React) 如何使用 node.js(express) API 调用访问搜索参数并在 React 前端提供服务 - How to access search params with node.js(express) API call and served on react front-end 如何同时运行我的前端反应和后端反应? - How do I run my react front-end and express back-end together? Java / Android前端客户端和节点js后端服务器? - Java/Android front-end client and node js backend server? 如何在前端应用程序和后端服务器之间共享Cookie? - How to share cookie between front-end app and backend server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM