'How to change port using fetch and request react js?

This is the code I took from my professor's tutorial. LoginComp is just {username: '', password: ''}

const request = new Request("/users/login", {
    method: "post",
    body: JSON.stringify(loginComp),
    headers: {
        Accept: "application/json, text/plain, */*",
        "Content-Type": "application/json"
    }
});

// Send the request with fetch()
fetch(request)
    .then(res => {
        if (res.status === 200) {
            return res.json()


        }
    })

    .catch(error => {
        console.log(error);
    });

I have a backend server running at localhost:5000 with post routes at localhost:5000/users/login. The issue with this code is it assumes users/login is at localhost 3000 so I would get this error in console:

POST http://localhost:3000/users/login 404 (Not Found)

How do I make it so its localhost:5000 not 3000 (the client server)



Solution 1:[1]

You have to add http://localhost:5000/ to your url.

const request = new Request("http://localhost:5000/users/login", {
    method: "post",
    body: JSON.stringify(loginComp),
    headers: {
        Accept: "application/json, text/plain, */*",
        "Content-Type": "application/json"
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kiranvj