React + Express: ошибка ECONNREFUSED при попытке получить данные с помощью fetch - PullRequest
0 голосов
/ 26 апреля 2018

Возвращаясь к узлу, но у меня возникают проблемы с подключением моего фронта (React) и назад (Node / Express).Я просмотрел статью David Ceddia на его сайте, и она работает, 100% работает с его примером.

Когда я пытаюсь перенести ту же логику в мое собственное приложение, я получаю

Proxy error: Could not proxy request /users from localhost:3000 to http://localhost/3001. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

Естественно, я нашел раздел с пометкой ECONNREFUSED и там написано

ECONNREFUSED (Connection refused): No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host.

Итак, соединение отказано, но яне могу понять, почему.

BACKEND

users.js (страница, с которой мы запрашиваем информацию)

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
  // Comment out this line:
  //res.send('respond with a resource');

  // And insert something like this instead:
  res.json([{
    id: 1,
    username: "samsepi0l"
  }, {
    id: 2,
    username: "D0loresH4ze"
  }]);
});

module.exports = router;

FRONT

package.json

{
  "name": "shinyspork",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-jss": "^8.4.0",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost/3001"
}

App.js

import React, { Component } from 'react';

class App extends Component {
  state = { users: [] }

  componentDidMount() {
    fetch('/users')
      .then(res => res.json())
      .then(users => this.setState({ users }));
  }

  render() {
    return (
      <div className="App">
        <h1>Users</h1>
        {this.state.users.map(user =>
          <div key={user.id}>{user.username}</div>
        )}
      </div>
    );
  }
}

export default App;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...