R
R
Radiss2020-01-05 04:55:07
MongoDB
Radiss, 2020-01-05 04:55:07

Error 500 when proxying requests - how to solve?

"Proxy error: Could not proxy request /api/auth/register from localhost:3000 to localhost:5000/. "
package.json on the client - on the backend I run through"proxy": "http://localhost:5000/",

"dev": "concurrently \"npm run server\" \"npm run client\""

When you try to register or sign in to the app
Proxy error: Could not proxy request /api/auth/register from localhost:3000 to http://localhost:5000/. [1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

In the browser the message is "unexpected token p in json at position 0"
In the console:
http.hook.js:15 POST http://localhost:3000/api/auth/register 500 (Internal Server Error)

Request Payload:

{email: "[email protected]", password: "dfgdgdhg"} email: "[email protected]" password: "dfgdgdhg"

Request URL: http://localhost:3000/api/auth/register Request Method: POST Status Code: 500 Internal Server Error Remote Address: 127.0.0.1:3000 Referrer Policy: no-referrer-when-downgrade

Proxy error: Could not proxy request /api/auth/register from localhost:3000 to http://localhost:5000/ (ECONNREFUSED).

http.hook.js
spoiler

import {useState, useCallback} from 'react'

export const useHttp = () => {
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState(null)

  const request = useCallback(async (url, method = 'GET', body = null, headers = {}) => {
    setLoading(true)
    try {
      if (body) {
        body = JSON.stringify(body)
        headers['Content-Type'] = 'application/json'
      }

      const response = await fetch(url, {method, body, headers})
      const data = await response.json()

      if (!response.ok) {
        throw new Error(data.message || 'Что-то пошло не так')
      }

      setLoading(false)

      return data
    } catch (e) {
      setLoading(false)
      setError(e.message)
      throw e
    }
  }, [])

  const clearError = useCallback(() => setError(null), [])

  return { loading, request, error, clearError }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-01-10
@radiss

what is there with the csrf token, is everything set up?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question