Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Cómo enviar valor entre React.js Frontend y Express.js Backend

Me gustaría enviar 'cookieValue' a mi servidor, pero no estoy muy seguro de cómo hacerlo y no he podido encontrar buenos recursos en línea.

Actualmente, no creo que mi 'cookieValue' realmente tenga un valor cuando intento enviarlo a mi servidor. También recibo una advertencia que dice que 'cookieValue' se declara pero nunca se usa en mi 'función findCookie'. A continuación tengo mi código actual, cualquier ayuda sería apreciada.

Código frontal:

 import axios from 'axios' import { Outlet, Navigate } from "react-router"; import { useState } from 'react' let backEndResponse = null function SendCookie() { const [cookieValue, setCookieValue] = useState(""); if (document.cookie != null) { const cookieValue = document.cookie .split('; ') .find(row => row.startsWith('Auth=')) .split('=')[1]; axios.post('http://localhost:5000/auth', { cookieValue, }).then(getBackEndResponse => { return axios.get('http://localhost:5000/auth', { withCredentials: true }).then((res) => { res.render(backEndResponse); }) }); } else { } } const useAuth = () => { if (backEndResponse) { const authorized = {loggedIn: true} return authorized && authorized.loggedIn; } else { const authorized = {loggedIn: false} return authorized && authorized.loggedIn; } }; const ProtectedRoutes = () => { const isAuth = useAuth(); return isAuth ? <Outlet/> : <Navigate to="/login" />; } export default ProtectedRoutes

Código de fondo:

 app.post('/auth', (req, res) => { const authCookie = req.body.cookieValue if (authCookie === result) { backEndResponse = true } else { backEndResponse = false } }); app.get('/auth', (req, res) => { res.render(backEndResponse) });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Las cookies provienen de su backend y el navegador las enviará automáticamente con cualquier solicitud posterior al mismo backend. Su backend puede acceder a ellos como res.cookies . No necesita escribir ningún código adicional para eso.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!