简体   繁体   中英

How to integrate a Nodejs API with ReactJs app under the same domain

I'm trying to understand how a MERN app fully works, I've been reading about MongoDB, ExpressJs, ReactJs and NodeJs, I also understand how MongoDB, ExpressJs and NodeJs interact and how ReactJs works on its own, my question is simple (I think).

The question: If I create an API, using Node,Express and Mongo, and I have an APP managed by React, both need a server (via express, I understand), then, how should I run the API and the React app at the same time. Do I need different URLs? should I configure different ports? how should I integrate them?

I really been reading a lot, but almost every tutorial is made locally (and I'm working in a server with Passenger and I can't change the way it starts), just for Node/Express(with pug or else)/Mongo or just React, and I don't understand how to connect the API and React.

Thanks

It depends on several factors: environment (eg development, production), and your control over the server. For development, you can have two different URLs and use something like Webpack Dev Server . Normally you would have the module bundler, eg Webpack, watching for changes in your React code. However, this can get more complex if you have Server Side Rendering.

For production, normally you would have the bundled file for your client side application already optimized and minified. If you can change your API, you could serve it statically in a new endpoint, for example: /static/bundle.js and request this endpoint from your index.html file, which will be sent by Express.js server when accessing / .

However, because you will probably want to have routes in your React app, your server will need to know how to handle the client app routes (for example app.get('/*', () => ...) , and they could collide with your API endpoints. To solve this, you could:

  • Prefix your API endpoints with a namespace, eg /api/v1/...
  • Place the API in a different URL, port or subdomain. In this case you would indeed need to run these two servers in parallel. With Node.js, there are helpers to make this more convenient, eg concurrently .

Pulling out your concerns: API , React , and Integration for MERN app.

I use three approaches

1) Use foreman . With this, you can specify your API and Web Client in the Procfile. I used it here

2) Use a proxy to handle requests that require your API. So in package.json , you specify your API URL(your API must be running)

// package.json
 .......
 .......
 "proxy": "<path to url:[port no if you're developing locally]>"

Check here . And you can simply add a script to run your API and React concurrently .

3) Set your API and React app in a Docker container. mern-starter is a perfect place to check for this.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM