//server
app.get('/', cors(corsOptions),async ( req,res )=>{
if (req.headers.cookie) {
let ck = req.headers.cookie.split('=')
const sendData = await mysql( 'list', ck[1] )
res.json( sendData[0] )
}else{
res.json( {badRequest:'cookie is not defined'} )
}
})
//client
export default function ApiData() {
const [usr,setUsr] = useState([])
useEffect(()=>{
let headers = {
'Access-Control-Allow-Origin':'*',
'accept': 'application/json',
'Cookie': document.cookie,
'origin':'same-origin'
}
fetch('http://localhost:8080',{ headers: headers })
.then(res => res.json())
.then(res => setUsr(res))
.then(res => console.log(res))
},[])
return(
<div>
{usr.badRequest}
{usr.name}
</div>
)
}
//even with the cookie set, on the frontend, it does not send the headers to the backend and enters the else => condition (on the backend it shows that the cookie is undefined)