Hello everyone, I’m sitting for 3 hours. I can’t connect the application with the server to the node (express). In the documentation, they write the proxy in package.json.
"name": "marmelad", "version": "0.1.0", "private": true, "proxy": "http://localhost:3001/", And he did.
Here is my ajax
import axios from 'axios'; export const userLoginRequest = (data) => dispatch => { // return axios.post('/api/auth', data); return fetch('/api/auth', { method: 'POST', headers: { 'Content-Type': 'text/html' }, body: data }) } I used axios, but I try to fetch it already, so in the documentation. Requests still go to localhost: 3000, instead of 3001.
On the server side everything is transparent
import express from 'express'; import bodyParser from 'body-parser'; // import cors from 'cors'; // import users from './routes/users'; import auth from './routes/auth'; const app = express(); app.use(bodyParser.json()); app.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }) app.set('port', (process.env.PORT || 3001)); // Express only serves static assets in production if (process.env.NODE_ENV === 'production') { app.use(express.static('client/build')); } app.use('/api/auth', auth); app.listen(app.get('port'), () => { console.log(`Find the server at: http://localhost:${app.get('port')}/`); }); auth.js
import express from 'express';
let router = express.Router(); router.post('/api/auth', (req, res) => { console.log(req.body); }) export default router;