N
N
Nikita Shchypylov2018-07-01 00:31:20
Node.js
Nikita Shchypylov, 2018-07-01 00:31:20

How to properly link Node.js and React?

Hello!
There are a couple of questions about setting up the connection between the back and the front.
1) How to make it so that react-router-dom does not lose context when updating the page? Let's say if I'm on /add, then stay on /add when updating? On express I use express.Router()
2) How can I run both front and back processes on the same port and do I need to do this for successful interaction?
The structure is as follows:
-package.json
-server
--server.js
--package.json
-frontend
--webpack.config.js
--package.json
The main package.json contains all the dependencies, the sub-folders contain only scripts. Made it possible to run both back and front from the main package.json :

"start": "concurrently \"npm run --prefix frontend start\" \"npm run --prefix server start\"",

Thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Javid Askerov, 2018-07-01
@Nikulio

I don’t understand what two processes are on the same port. In any case, you can't.
And it is done so, the express is already there. The script is launched, the front is collected in the dist/ folder, for example, from this folder index.html is served by express to any page using the .route("*", "index.html") flag.
That is, for any request, you give index.html, in which the compiled React application is already connected, where react-router-dom is already connected and decides all the paths itself. And on the server you keep your API endpoints.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question