• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

269
Views
Cómo obtener datos y hacer una ruta con el mismo punto final Reaccionar

Tengo un punto final desde donde recupero datos, es /community.
Mi aplicación de reacción usa el puerto 3001 pero el servidor de nodo donde está el punto final de la comunidad usa el puerto 3000, el problema es que cuando intento enrutar a localhost: 3001/comunidad para mostrar un componente, me da los datos json del servidor, pero necesito mostrar el componente, ¿podría ayudarme a identificar y solucionar el problema, por favor?
setupProxy.js:

 const { createProxyMiddleware } = require('http-proxy-middleware'); module.exports = function (app) { app.use( createProxyMiddleware('/subscribe', { target: 'http://localhost:3000', changeOrigin: true, headers: { Connection: "keep-alive" } }) ); app.use( createProxyMiddleware('/unsubscribe', { target: 'http://localhost:3000', changeOrigin: true, headers: { Connection: "keep-alive" } }) ); app.use( createProxyMiddleware('/community', { target: 'http://localhost:3000', changeOrigin: true, headers: { Connection: "keep-alive" } }) ); }

Aplicación.js:

 import './App.css'; // import JoinUsSection from './components/JoinUsSection'; // import BigCommunitySection from './components/BigCommunitySection'; import { Provider } from 'react-redux'; import { store, persistor } from './store'; import { PersistGate } from 'redux-persist/integration/react' import { fetchUsers } from './asyncActions/users'; import {Routes,Route} from 'react-router-dom' import Main from './pages/Main' import Community from'./pages/Community' store.dispatch(fetchUsers()) function App() { return ( <Provider store={store}> <PersistGate loading={null} persistor={persistor}> <Routes> {/* <main id='app-container'> <BigCommunitySection /> <JoinUsSection /> </main> */} <Route path="/" element={<Main/>}/> <Route path="/community" element={<Community/>}/> </Routes> </PersistGate> </Provider> ); } export default App;

Comunidad.js:

 import BigCommunitySection from '../components/BigCommunitySection'; const Community = () =>{ return( <BigCommunitySection/> ) } export default Community

Y users.js donde busco datos:

 import axios from "axios" import { addUsers } from "../store/usersReducer" export const fetchUsers = () => { return dispatch => { axios .get('/community') .then(response => { dispatch(addUsers(response.data)) }) .catch(error => { console.log(error) }) } }
over 3 years ago · Santiago Trujillo
1 answers
Answer question

0

Un patrón común es prefijar todas sus URL de proxy con /api o similar.

Por ejemplo

 module.exports = function (app) { app.use("/api", createProxyMiddleware({ target: 'http://localhost:3000', pathRewrite: { "^/api": "" // remove the /api prefix }, changeOrigin: true, headers: { Connection: "keep-alive" } }) ) }

Esto transferirá cualquier solicitud que comience con /api a los puntos finales de su servidor Node, eliminando el prefijo /api , es decir

  • /api/subscribe => http://localhost:3000/subscribe
  • /api/unsubscribe => http://localhost:3000/unsubscribe
  • /api/community => http://localhost:3000/community

Luego haga sus solicitudes a /api -URL prefijadas

 axios.get("/api/community")

Puede hacer esto aún más fácil configurando una instancia de Axios con la baseURL adecuada

 const api = axios.create({ baseURL: "/api" }) api.post("/subscribe") api.post("/unsubscribe") api.get("/community")
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error