score:0
npm-run-all
package will help you to accomplish this task.
There is an option that can set in Create React App's package.json that proxies non text/html requests through to an alternative back end. You can use this feature to proxy to applications running elsewhere, using this you will be able to run a server within the React project itself.
Add below line to package.json file : "proxy": "http://localhost:3001",
Modify the scripts section as below :
"scripts": {
"start": "react-scripts start",
"server": "node-env-run server --exec nodemon",
"dev": "run-p server start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Finally your package.json file will be looked like this.
{
"name": "frontend_backend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.8",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
"nodemon": "^2.0.6",
"npm-run-all": "^4.1.5",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"proxy": "http://localhost:3001",
"scripts": {
"start": "react-scripts start",
"server": "node-env-run server --exec nodemon",
"dev": "run-p server start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Now, use npm run dev
to run the application. (You may change 'dev
' to anything that you want e.g : "app": "run-p server start"
, then use npm run app
command to run the application)
score:0
Install npm concurrently package in the backend
Add below script inside backend's package.json
"start": "node server.js", "server": "nodemon server.js", "frontend": "cd ./frontend && npm run dev", "dev": "concurrently \"npm run server\" \"npm run frontend\""
Make sure you provided the correct path for the client and server
Add "proxy": "localhost:5000" as a proxy to package.json file of your react app. assuming your nodejs app is running on port 5000.
Run 'npm run dev' from the backend root folder
score:1
One Possible solution to run simuntaneously is
First you need to change proxy to 5000
"proxy": "http://localhost:5000",
make sure the folder structure is Something like this
use concurrently in the backend( outside) package.json :
"scripts": {
"start": "node backend/server.js",
"client": "npm start --prefix frontend",
"dev": "concurrently "npm run start" "npm run client""}
score:2
Install package npm-run-all, which helps you to execute multiple scripts. You can refer the below link:
After installing this package, In your package.json, add the script like this:
"scripts": {
"start-js": "react-scripts start",
"backend-start": "NODE_ENV=production node node_api/server.js",
"start": "concurrently \"npm-run-all -p backend-start start-js\"",
"build": "npm-run-all build-css && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Here, when you run the command "npm start", it first run the "backend-start" script which starts your backend server and then it starts react.
Just change the path in "backend-start" script. replace "node_api/server.js" with your path node startup file
score:4
In 5 steps:
- Install
npm i --s concurrently
package - Add below script inside Node/server/backend's package.json
"client": "npm start --prefix client",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client"
- Make sure you provided the correct path for client and server
- Run 'npm run dev'
- Smile
Source: stackoverflow.com
Related Query
- How can I start my node server and react app at the same time?
- how to run my node application and react app in the same time with one command?
- Can I use mock data and proxy api data at the same time in my react project when using node express as a proxy server?
- React tutorial - how do I start the node server for a reactJs application?
- How to use the same port for React Js and Node Js?
- React/webpack - How can I host react app on one server and images/fonts on another server?
- How to run client and server code at the same time
- React Typescript: How run some lines at the same time as each other, and other parts need to wait
- How to boot up react and node api server on same port?
- How to serve React app and Api on same node server?
- How can I use the same React app in multiple locations of the same page?
- How to send data and photo at same time to node js backend using axios in react js
- How to host a static landpage and react on the same node server?
- How can I change the URL for a React app that I'm hosting myself and to which I forward a domain?
- how to add simple className and fucntion return at the same time in react
- How should I update both array and <ul> at the same time using react hooks?
- Trying to run my node.js server at same time as React .js App and get: 'ERR_MODULE_NOT_FOUND'
- How to combine the previous data with the new data in React and display it at the same time
- How can I start my app in CRA and have it open with the Edge browser?
- How can I prevent React and Plotly crashing the development server due to JavaScript heap out of memory?
- Upload file (image) and data at the same time with react and node
- How can I pass an array and an object at the same time to axios.post
- How to deploy React app and Nodejs backend on the same directory in the subdomain?
- React Materials UI - How can I close two dialogs at the same time
- How to place text at the start and another at end in the same alert tage in react bootstrap
- How to start node server when React app starts in production
- How can I show sorting icons all the time and with blue color for current sorting column in react material-table
- react hook, how to highlight current row on click and un-highlight other rows at the same time
- how can i focus only one button at a time and not several at the same time?
- How can I update react app that gets it's data from the node mongo backend using redux
More Query from same tag
- Dispatch another action / call another reducer within a reducer
- Clone a React component that implements forwardRef
- How does JSX render the {foo => [ <bar />, <baz /> ]} syntax?
- Trying to use react-admin transform function on <Create />
- Avoiding Empty Curly Braces in MongoDB Aggregation
- TypeError: urls.map is not a function in React
- StringWriter processes strange characters on csv generated
- Trouble requiring too many images in React Native
- Unable to find type definitions when using typescript 3.0.1 project refferences
- Update UI when useRef Div Width Changes
- React Component Testing with Cypress - Where does 'type' come from?
- How to filter array from one date to another in Reactjs
- I get "no such method" when using ES6 static functions
- Required textarea is being highlighted in red before submitting form
- React component with ajax call - life-cycle
- Passing item through props
- How to uncheck the checked element on removal of its tag in react js
- Express server doesn't start
- Axios with firebase does not return the data while putting it
- Added 2 Schema in Yup Validation React-Hook-Form
- how can i add an icon to HTML nested in JS
- Where should the response logic happen, in the saga or the reducer?
- Why does TypeScript assertion of object literal `{a}` work with interface `{a, b}` but not `{a?, b}`
- Wrapping a high order component(HOC) around a React component in JSX
- How to use javascript in react component
- Redux-Saga: Is there a guaranteed order of saga execution?
- Cant overwrite the custom stylesheet
- Unexpected token React / JSON
- Basic react multi-page app changing Header contents
- Warning: Invalid attribute name: `'data-*'`