score:0

You specify the api url relative to the current page url:

...
const { data } = await axios.get(`api/products/${productId}`);
...

// the api url will change depending on the url of the current page 
// http://localhost:3000 -> http://localhost:3000/api/products/${productId}
// http://localhost:3000/product -> http://localhost:3000/product/api/products/${productId}
// http://localhost:3000/about -> http://localhost:3000/about/api/products/${productId}
// ...etc

Just add a leading slash for the api address. It ensures the path is absolute to the root directory and not the current directory

...
const { data } = await axios.get(`/api/products/${productId}`);
...

Related Query

More Query from same tag