简体   繁体   English

使用 Reactjs 应用程序作为域后端中 express nodejs 的前端

[英]Use Reactjs app as a frontend for express nodejs in backend on domain

I created a frontend app using react js that calls an express node js api in the backend, in localhost i don't have problems because it runs on localhost, but when i deploy them both on plesk server, i don't know how to call express api from my react js app.我使用 react js 创建了一个前端应用程序,该应用程序在后端调用 express 节点 js api,在 localhost 中我没有问题,因为它在 localhost 上运行,但是当我将它们都部署在 plesk 服务器上时,我不知道如何从我的反应 js 应用程序调用 express api。

1.How to run express node js in production on plesk server. 1.如何在plesk服务器生产中运行express node js。 2.How to call express api from react js app (in localhost i use http://localhost:8000/users ) 2.如何从 react js 应用程序调用 express api(在本地主机中我使用http://localhost:8000/users

Thank you kindly非常感谢你

This deployments assumes, you build your react app into your express app.此部署假设您将 React 应用程序构建到您的快速应用程序中。 So in final, your app runs on the same domain as your backend.所以最终,您的应用程序与您的后端在同一个域上运行。 Eg if you build your react app to reactapp folder within your express, then you serve it with:例如,如果您将 react 应用程序构建到 express 中的reactapp文件夹,那么您可以使用:

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'reactapp/index.html'));
});

And on the same express you have another route for your api:在同一条快车上,您的 api 有另一条路线:

app.get('/api/somerestapi', (req, res) => {
  // process your api request 
  res.send({data: 'some data'});
});

It means, you call your API from React with relative path:这意味着,您使用相对路径从 React 调用 API:

const request = new XMLHttpRequest();
request.open('GET', '/api/somerestapi);
...

暂无
暂无

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

相关问题 使用 NodeJS 后端(使用 express)为 heroku 上的服务器/数据库部署 ReactJS 应用程序 - Deploying ReactJS app with NodeJS backend (using express) for server/database on heroku 当前端(ReactJS)和后端(NodeJs,Express)位于两个不同的文件夹中时,如何运行 npm run build - How to run the npm run build, when frontend (ReactJS) and backend (NodeJs,Express) are in two different folders 如何从同一个项目文件夹部署ReactJS前端和NodeJS / Express后端? - How to deploy ReactJS frontend and NodeJS/Express backend from the same project folder? 使用 ReactJS 前端和 NodeJS 后端实现 webhooks - Implement webhooks with ReactJS frontend and NodeJS backend MySQL与NodeJS后端和ReactJS前端的集成 - Integration of MySQL with NodeJS Backend and ReactJS FrontEnd 在 Reactjs + Nodejs App 中,React 前端没有收到来自 nodejs 后端的返回 - React frontend doesn't receive return from nodejs backend in Reactjs + Nodejs App 带有 nodejs/express 后端的 ReactJS 应用程序 - 无法让 api 后端工作 - ReactJS app with nodejs/express for backend - can't get api to backend working 使用 reactjs 作为前端和 nodejs 作为后端时 cookie 为空 - cookie is null when using reactjs as frontend and nodejs as backend 如何在 firebase 上正确部署带有 NodeJS 后端的 ReactJS 前端 - How to properly deploy a ReactJS frontend with a NodeJS backend on firebase 如何将前端 ReactJs 项目文件夹与 NodeJs 后端连接? - How to connect Frontend ReactJs Project Folder with NodeJs Backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM