React code
import './App.css';
import {useState,useEffect} from 'react';
import Todos from './Components/Todos';
function App() {
const [todo, setTodo] = useState([]);
const conn=()=>{
console.log('h')
// fetch('https://jsonplaceholder.typicode.com/users')
fetch("http://localhost:5000/about", {
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(response => response.json())
.then(json => console.log(json))
.catch((err)=>console.log(err));
}
// useEffect(() => {
// fetch('https://jsonplaceholder.typicode.com/users')
// .then(response => response.json())
// .then(json => setTodo(json))
// }, [todo])
return (
<>
<div>
{
// todo.map((data)=>{
// return <Todos data={data}/>
// })
}
</div>
<button onClick={conn}>Connect with server</button>
</>
);
}
export default App;
Nodejs code
const express=require('express');
const app=express();
const PORT=process.env.PORT||5000;
const route=require('./paths');
const cors=require('cors');
const bodyParser=require('body-parser');
app.use(bodyParser.json());
app.use(cors());
app.get('/about',(req,res)=>{
console.log('Alhumdulillah');
console.log(req.body)
res.json({"name":"md"});
})
app.post('/p',(req,res)=>{
console.log( req.json());
// console.log(x.json());
res.send('Thanks for calling me');
})
app.listen(PORT,()=>{
console.log("server is running at "+PORT)
});
Comments
Post a Comment