[英]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.