This is the full react code where I fetch the data to the server:
//process.env.NEXT_PUBLIC_DR_HOST being http://localhost:3000
await fetch(`${process.env.NEXT_PUBLIC_DR_HOST}/validate`, {
method: 'POST',
body: valBody,
headers: { 'Content-Type': 'application/json' }
}).then(res =>
{
if (res.status === 200)
{
body
? fetch(`${process.env.NEXT_PUBLIC_DR_HOST}/view/${_id}`, {
method: 'PUT',
body: body,
})
: fetch(`${process.env.NEXT_PUBLIC_DR_HOST}/view/${_id}/raw`, {
method: 'PUT',
body: valBody,
headers: { 'Content-Type': 'application/json' }
})
}
}).then(res =>
{
// console.log(res)
// window.location = res.url;
})
We are focused on the scenario where body is null (the problem appears in both). So this is the main part:
...
: fetch(`${process.env.NEXT_PUBLIC_DR_HOST}/view/${_id}/raw`, {
method: 'PUT',
body: valBody,
headers: { 'Content-Type': 'application/json' }
})
...
.then(res =>
{
// window.location = res.url;
})
The data is sent to the server, and the page redirected using the response parameter (res.redirect("/")) and the returned promise:
.then(res =>
{ //res.url = http://localhost:3000
window.location = res.url;
})
I get redirected to the specified url and practically everything works fine as it should. But here's the problem: When using the res.redirect() function (as in this case), I also get a 404 error with the message PUT http://localhost:3000/ 404 (Not Found), even if everything worked as expected, the url included in the error message being always the same as the url redirected to.
Useful:
When sending something like res.status without redirecting - nothing happens, but when sending back a message res.send("AAAA") (deos not matter if res.redirect is used) , the problem is invoked again (). So the res.redirect and res.send are causing the error. Sounds strange but the error url when using the res.send function alone is the same applied last time when res.redirect was triggered. (If I redirected res.redirect("/view"), I get and error with the "/view" url, and when replacing the line of code with res.send("anything"), the same error with the same url is printed )
I don't really know what to do