score:-1

first, check the cors version, if it doesn't work, try to use a proxy.

you can use proxy as follow,

in your front-end, pacakge.json just add this line of code.

  "proxy": "http://localhost:5000",

and in your axios.get remove thie http://localhost:5000 and

   axios.get("http://localhost:5000/api/products");

instead, do it like this

    const res = axios.get("/api/products");

and if both doesn't work, then try to install and import body-parser on your server-side.

installtion:
npm i body-parser

import:
const bodyparser = require('body-parser')

score:-1

try creating a file called: setupproxy.js within your src folder in client. install http-setup-proxy

add this to setupproxy.js:

const { createproxymiddleware } = require('http-proxy-middleware')
module.exports = function (app) {
    app.use(
        '/api',
        createproxymiddleware({
            target: 'http://localhost:5000',
            changeorigin: true,
        }),
    )
}

finally, restart your application.

score:0

the problem is that you include the cors middleware at the bottom of your middleware stack, so it would only ever be reached if none of the routes match, i.e. your 404 errors would have cors headers but nothing else would:

app.use("/api/auth", authroute);
app.use("/api/users", userroute);
app.use("/api/products", productroute); // this route handles the request
                                        // and ends the request processing
app.use("/api/carts", cartroute);
app.use("/api/orders", orderroute);
app.use(cors());                        // this is never reached!

you need the cors headers always, so you have to put the middleware at the top of the stack:

app.use(cors());                        // this is executed first, adds the
                                        // headers and continues processing
app.use("/api/auth", authroute);
app.use("/api/users", userroute);
app.use("/api/products", productroute); // this route handles the request
app.use("/api/carts", cartroute);
app.use("/api/orders", orderroute);

Related Query

More Query from same tag