简体   繁体   English

如何在同一域下将Nodejs API与ReactJs应用程序集成

[英]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). 我正在尝试了解MERN应用程序如何完全运行,我一直在阅读有关MongoDB,ExpressJ,ReactJ和NodeJ的信息,我也了解MongoDB,ExpressJ和NodeJ的交互方式以及ReactJ如何独立工作,我的问题很简单(我认为)。

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. 问题:如果我使用Node,Express和Mongo创建一个API,并且我有一个由React管理的APP,两者都需要一个服务器(通过Express,据我所知),那么我应该如何在以下位置运行API和React应用同一时间。 Do I need different URLs? 我需要不同的URL吗? 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. 我确实读了很多书,但是几乎每个教程都是本地制作的(而且我正在与Passenger一起在服务器上工作,我无法更改其启动方式),仅用于Node / Express(带有pug或其他)/ Mongo或只是React,而我不了解如何连接API和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 . 为了进行开发,您可以有两个不同的URL并使用Webpack Dev Server之类的东西。 Normally you would have the module bundler, eg Webpack, watching for changes in your React code. 通常,您将使用模块捆绑器(例如Webpack)来监视React代码中的更改。 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 / . 如果可以更改API,则可以在新端点(例如/static/bundle.js静态为其提供服务,并从您的index.html文件请求该端点,该文件将由Express.js服务器在访问/时发送。

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: 但是,由于您可能希望在React应用程序中包含路由,因此您的服务器将需要知道如何处理客户端应用程序路由(例如app.get('/*', () => ...) ,以及它们可能与您的API端点冲突。要解决此问题,您可以:

  • Prefix your API endpoints with a namespace, eg /api/v1/... 给API端点添加名称空间前缀,例如/api/v1/...
  • Place the API in a different URL, port or subdomain. 将API放在其他URL,端口或子域中。 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 . 使用Node.js,可以使用一些帮助程序来使此操作更加方便,例如, 并发

Pulling out your concerns: API , React , and Integration for MERN app. 消除您的疑虑:针对MERN应用的APIReactIntegration

I use three approaches 我用三种方法

1) Use foreman . 1)使用工头 With this, you can specify your API and Web Client in the Procfile. 这样,您可以在Procfile中指定您的API和Web客户端。 I used it here 在这里用过

2) Use a proxy to handle requests that require your API. 2)使用代理处理需要您的API的请求。 So in package.json , you specify your API URL(your API must be running) 因此,在package.json ,您指定您的API URL(您的API必须正在运行)

// 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 . 你可以简单地添加一个脚本来运行你的API ,并React 的同时

3) Set your API and React app in a Docker container. 3)在Docker容器中设置API和React应用。 mern-starter is a perfect place to check for this. mern-starter是检查此问题的理想场所。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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