#React connect to API Rest

11 messages · Page 1 of 1 (latest)

near veldt
#
  1. you should be using fetch('/cregister/api/customer/list') (don't include the protocol/host/port)
  2. for your react development server (the thing running on port 3000), you should configure a proxy to redirect all API requests to port 3001
  3. you should not be using a react development server in production. for production, you "build" your react app and let the server running on port 3001 serve the built files.
#

(also learn async/await)

delicate fern
# near veldt 1. you should be using `fetch('/cregister/api/customer/list')` (don't include th...

Thanks , i implemented setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {
    app.use(
        '/cregister',
        createProxyMiddleware({
            target: 'http://localhost:3001',
            changeOrigin: true,
        })
    );
};

About "you should not be using a react development server in production. for production, you "build" your react app and let the server running on port 3001 serve the built files."
Sorry, I don't get it.
Actually i use npm run build and npm start for my app.
So what i should do to get my app working with API Rest ?

near veldt
#

Are you using CRA?

delicate fern
near veldt
#

...

#

if you're using your own custom framework and/or build system, glhf... if you're using CRA, Vite, or something somebody else has used, somebody can probably help.

#

what part of what I said do you not understand? do you get what is happening when you type npm run start or npm run build?

delicate fern
#

I followed a course thats why i did like this.
I will rewrite my app by using CRA.

near veldt
#

Vite is preferred these days

delicate fern