Answer the question
In order to leave comments, you need to log in
How to make friends React and Backend part on Node.js?
I just started developing applications, I can't figure it out. How to connect frontend - on React and server on Node.js?
Remotely - I bought a VPS, set it up, uploaded node.js , launched it. I opened my React application from the browser, sent an AJAX request, and I received a response from my server. OK. How can I develop everything locally? I have run npm node app.js application on port 8000. I start my react app with npm start, it opens on port 3000. But Ajax requests don't get any response. I tried setting the headers "Content-Type": "application/json",
"Access-Control-Allow-Origin":"*" , on the remote server it gave the result. Locally, it didn't. How to develop locally?
Answer the question
In order to leave comments, you need to log in
How much I (dis)like these questions about node.js and ports.
Install nginx on a localhost that correctly routes requests from port 80 to port 3000 or 8000, depending on the request, and enjoy yourself.
How right? Yes, just like you set up the VPS.
But Ajax requests don't get any response.
Drumroll, Solution!
http.createServer((req,res)=>{
res.writeHead(200, {
"Access-Control-Allow-Origin":"*",
"Content-Type": "application/json",
"Access-Control-Allow-Credentials":"true"
});
res.end(JSON.stringify({newName:"Hello World"}))
}).listen(3001);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question