3
3
33remido2021-09-25 19:04:30
React
33remido, 2021-09-25 19:04:30

Why returns Failed to fetch?

POST https://localhost:5000/add-user/ net::ERR_CONNECTION_CLOSED

Uncaught (in promise) TypeError: Failed to fetch

I make a request through my hook, please tell me what could be the problem?
CLIENT
const useDb = useDatabase();
<button onClick={() => useDb.addUser(userInfo.email, userInfo.name, userInfo.password)}>Submit</button>


HOOK
export default function useDatabase() {

    function addUser(email, name, password) {
        let newUser = {
            email: email,
            name: name,
            password: password
        }
        fetch(`https://localhost:5000/add-user/`, {
            method: 'POST',
            headers: {'Content-type': 'application/json'},
            body: JSON.stringify(newUser)
        }).then(data => console.log(data));
    }

    return {addUser}
}


SERVER
const dotenv = require('dotenv');
const dbService = require('./dbService');
const cors = require('cors');
dotenv.config();

const express = require('express');

app = express();
app.use(cors());
app.use(express.json());
const port = process.env.SERVER_PORT;

app.listen(port, () => {console.log('server started on port ' + port)})

app.post('/add-user', function(req, res) {
    console.log(req.body)
});

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey, 2021-09-26
@33remido

I suspect that the problem is in https and the request http://localhost:5000/add-user/will work. For https, you need an ssl certificate, but in the server code I don’t see the creation of an https server.

T
Tyranron, 2014-10-18
@maxloyko

While writing, you were moving in the same direction =).
Here's a bike:

function array_merge_sorted($first, $second)
{
    $out = [];
    for (
        $f = 0, $s = 0,
        $firstLength = count($first),
        $secondLength = count($second);
        $f < $firstLength || $s < $secondLength;
    ) {
        if (!isset($first[$f])) {
            $out[] = $second[$s];
            ++$s;
        } elseif (!isset($second[$s])) {
            $out[] = $first[$f];
            ++$f;
        } elseif ($first[$f] > $second[$s]) {
            $out[] = $second[$s];
            ++$s;
        } else {
            $out[] = $first[$f];
            ++$f;
        }
    }
    return $out;
}

By time: 1 pass through the first array + 1 pass through the second array.
From memory: 1 size of the first array + 1 size of the second array, no extra allocations.

S
Sali_cat, 2014-10-18
@Sali_cat

well, no, it's not.

K
KorsaR-ZN, 2014-10-18
@KorsaR-ZN

You are not like an array without re-sorting, because after merging, you will need to sort it again at the output.
Make a pass through the arrays, and insert values ​​one after the desired values ​​of the second, while constantly remembering the index of the value in another array in order to skip unnecessary iterations of the search for the insertion position. I think the idea is clear.
The correct solution is to apply the " Merge Sort " algorithm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question