简体   繁体   English

Nodejs和ReactJs在同一台服务器上一起工作

[英]Nodejs and ReactJs work together on same server

Node and react runs on separate ports, for API requests form react app we can proxy to node URL. 节点和React在单独的端口上运行,对于来自React应用的API请求,我们可以代理到节点URL。

I don't need to do react server-side rendering to serve react app, so I build the react app (for every change in react app) and serving build/index.html via node after authentication. 我不需要做react服务器端渲染来为react app提供服务,因此我构建了react应用(针对react应用中的每个更改)并在身份验证后通过节点提供了build / index.html。

Problem 问题

I build react app for every change. 我为每一个变化构建了反应应用程序。

Is there any workarounds to serve the app via node that is running with live reloading? 是否有任何变通办法通过实时重新加载运行的节点来为应用程序提供服务?

Also, anyone knows where is the location of files generated when running react-scripts start , so i can serve from that location, right? 另外,任何人都知道运行react-scripts start时生成的文件的位置在哪里,所以我可以从该位置提供服务,对吗?

this is just a mad thought. 这只是一个疯狂的想法。

I would like to know any solutions for this problem? 我想知道这个问题的解决方案吗?

I figured out to make it work via ngrok and proxy. 我想通通过ngrok和代理使其工作。

Here is the full answer Shopify app with reactjs and nodejs without nextjs? 这是带有reactjs和nodejs而没有nextjs的Shopify应用的完整答案

You may have two folders with client and server application. 您可能有两个包含客户端和服务器应用程序的文件夹。 just command ng init and generate a package.json file in your main folder which contains client and server. 只需命令ng init并在包含客户端和服务器的主文件夹中生成package.json文件。 Now command npm install which generate nodemodules folder. 现在命令npm install生成nodemodules文件夹。

Now edit your package.json as:- 现在将package.json编辑为:-

"scripts": {
  "client": "cd client && npm start",
  "server": "cd server && npm start",
  "start": "concurrently - kill-others \"npm run server\" \"npm run client\""
  },
"devDependencies": {
  "concurrently": "3.5.1"
 }

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

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