简体   繁体   中英

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.

1.How to run express node js in production on plesk server. 2.How to call express api from react js app (in localhost i use http://localhost:8000/users )

Thank you kindly

This deployments assumes, you build your react app into your express app. 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:

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

And on the same express you have another route for your 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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