I built 2 files. One file is my react app what is oppened at 3000 port, and second is my NodeJs server what is oppened at 4000 port.
//Here is my nodejs file
var express = require('express');
var app = express();
app.post('/', function (req, res) {
res.send('POST request to the homepage');
});
app.listen(4000, function () {
console.log('Example app listening on port 4000!');
});
//Here is my react page
import React, { useState, useEffect } from 'react';
const Home = () => {
useEffect(async function () {
const url = 'http://localhost:4000/';
const response = await fetch(url);
const data = await response.json();
console.log(data)
});
return(
<div>
<h1>Home Page</h1>
<p>{data}</p>
</div>
)
}
export default Home;
How to send POST request to the homepage from nodejs file to my reactjs file? I tried with fetch but i dind't find a solution to solve the issue. Who know how to do this?
dataisn't seen from outside the effect, 3) I doubt that you set CORS on your server..