Показать сообщение отдельно
  #1 (permalink)  
Старый 08.06.2019, 06:53
Аспирант
Отправить личное сообщение для gsdev99 Посмотреть профиль Найти все сообщения от gsdev99
 
Регистрация: 03.02.2019
Сообщений: 72

Как правильно настроить proxy?
Всем привет. Подскажите, пожалуйста, как правильно настроить proxy,
На сервере есть вот такой route:
app/server/.js
app.get('/api/product/brands', (req, res) => {
    console.log('/api/product/brands');

    Brand.find({}, (err, brands) => {
        if (err) {
            return res.status(400).send(err);
        }

        res.status(200).send(brands);
    })
});


app/client/src/App.js (create-react-app)
import React from 'react';
// import logo from './logo.svg';
// import './App.css';
import axios from 'axios';

class App extends React.Component {
    componentDidMount() {
        axios.get('/api/product/brands').then(response => {
            console.log(response);
        });
    }

    render() {
        return <div className="App">MY APP</div>;
    }
}

// function App() {
//   return (
//     <div className="App">
//       <header className="App-header">
//         <img src={logo} className="App-logo" alt="logo" />
//         <p>
//           Edit <code>src/App.js</code> and save to reload.
//         </p>
//         <a
//           className="App-link"
//           href="https://reactjs.org"
//           target="_blank"
//           rel="noopener noreferrer"
//         >
//           Learn React
//         </a>
//       </header>
//     </div>
//   );
// }

export default App;


app/client/package.json
{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "proxy": {
    "/api/": {
      "target": "http://localhost:3002"
    }
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}


И при запросе:
GET [url]http://localhost:3000/api/product/brands[/url] 404 (Not Found)
Error: Request failed with status code 404
"proxy": {
  "/api/": {
    "target": "http://localhost:3002"
  }
},
Ответить с цитированием