I\'m builing a MERN application with create-react-app (so no custom webpack) and a node server. I\'m using nodemon to restart changes on the backend, and the problem is that abo
Try using CORS
instead of a package.json
proxy. I remember having random/intermittent connection issues when I used one. In short: Front-end runs on port 3000
and express
API runs on 5000
. When compiled, both run on 5000
and express
serves the compiled front-end js and acts like an API.
Very similar set up, but no connection problems:
express server package.json
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "NODE_ENV=production node app.js",
"server": "NODE_ENV=development nodemon app.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"seeds": "NODE_ENV=development node seeds.js"
},
...
client/package.json (using a sass compiler, which you can use/ignore).
...
"scripts": {
"build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
...
client/src/actions/axiosConfig.js (then I create an axios
config to automatically point to my Express API running on 5000
)
import axios from 'axios';
export const app = axios.create({
baseURL: 'http://localhost:5000/api/',
withCredentials: true
})
client/src/actions/authActions.js (then import the axios
config)
import { app } from './axiosConfig';
const signinUser = props => dispatch => (
app.post(`signin`, { ...props })
.then(({data}) => {
dispatch({ type: types.SET_SIGNEDIN_USER, payload: data })
dispatch(fetchAvatarOnLogin());
})
.catch(err => dispatch({ type: types.SERVER_ERROR, payload: err }))
);
express server.js (I use consign
to import all the files):
const express = require('express');
const app = express();
const consign = require('consign');
consign({ locale: 'en-us', verbose: false})
.include('libs/middlewares.js')
.then("database")
.then("shared")
.then("services")
.then("controllers")
.then("routes")
.then('libs/server.js')
.into(app);
However, the equivalent would be:
// APP REQUIRED IMPORTS
const express = require('express');
const app = express();
...etc
// APP MIDDLEWARES
...
app.use(cors({credentials: true, origin: http://localhost:3000})) // allows receiving of cookies from front-end
app.use(morgan('tiny')); // logging framework
app.use(bodyParser.json()); // parses header requests (req.body)
app.use(bodyParser.urlencoded({ extended: true })); // allows objects and arrays to be URL-encoded
...etc
// DATABASE CONFIG/CONN
// APP SHARED FUNCS
// APP SERVICES (passport, sendgrid mailer, ...etc)
// APP CONTROLLERS
...
signin: (req, res, done) => passport.authenticate('local-login', err => (
(err || !req.session) ? sendError(err || badCredentials, res, done) : res.status(201).json({ ...req.session }))
)(req, res, done)
...etc
// APP ROUTES
...
app.post('/api/signin', signin);
...etc
// EXPRESS SERVER
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => res.sendFile(path.resolve('client', 'build', 'index.html')));
}
app.listen(5000);