简体   繁体   English

如何从Express Server开始运行react-scripts?

[英]How to run react-scripts start from an express server?

I'd like to run a react app served by an express server. 我想运行一个由Express服务器提供服务的react应用。 Even though it's not the solution, the effect what I'd like to earn is app.use("/", "react-scripts start") , so if the server gets a request at "/" , it starts the react app, and serves it's files. 即使不是解决方案,我想要获得的效果还是app.use("/", "react-scripts start") ,因此,如果服务器在"/"处收到请求,它将启动react应用,并提供文件。

I read about the solution of building the app, and serve bundle.js, or just adding a proxy with the server's URL to the client app's package.json, but that's not what I want, and haven't found anything similar to the effect I'd like to earn. 我阅读了有关构建应用程序,提供bundle.js或仅将具有服务器URL的代理添加到客户端应用程序的package.json的解决方案的信息,但这不是我想要的,并且没有发现与效果类似的内容我想赚。

I'm not sure what react-scripts start does, and how it's working, but the reason why I need it is that I don't want to restart the whole server, and wait until the app builds every time I change something in the front-end. 我不确定react-scripts start什么react-scripts start以及它如何工作,但是我之所以需要它,是因为我不想重新启动整个服务器,而每次我更改应用程序中的内容时都等到应用程序生成为止前端。

Any ideas? 有任何想法吗?

What exactly is the outcome you're trying to achieve? 您要达到的结果到底是什么? Using create-react-app (for which react-scripts is a utility), or webpack in general, the app must always be rebuilt at some point for the changes to be seen, since JSX is not browser-compatible and must be transpiled to regular JS. 通常,使用create-react-app (对于react-scripts是一个实用工具)或webpack,必须始终在某个点重建该应用程序以使更改可见,因为JSX与浏览器不兼容并且必须转换为常规JS。 Also, it's not necessary to restart the server if you're only making changes to the front end. 另外,如果仅对前端进行更改,则无需重新启动服务器。

Running react-scripts start basically runs webpack-dev-server (WDS) which is built into CRA's configuration. 运行react-scripts start基本上会运行内置在CRA配置中的webpack-dev-server (WDS)。 WDS does not actually build the project in the sense of outputting build files, but it still must build it in-memory to be able to even display the changes at all. 从输出构建文件的意义上讲,WDS实际上并没有构建项目,但是它仍然必须在内存中进行构建,以便甚至可以显示所有更改。 On a normal application, rebuilding through WDS shouldn't take more than a handful of seconds, and that's about the fastest feedback loop you're going to get for seeing changes manifest. 在正常的应用程序中,通过WDS进行重建的时间应该不会超过几秒钟,这就是您看到变化显而易见的最快反馈循环。

If you're running an Express server alongside a CRA app, I'd recommend looking into concurrently and nodemon , the former of which will allow you to run your server and React client with one command, and the latter of which will automatically restart your server for you (only when back end changes are made, a front end change will not trigger a server restart). 如果您将Express服务器与CRA应用程序一起运行,建议您同时查看nodemonnodemon ,前者将允许您使用一个命令来运行服务器和React客户端,后者将自动重新启动您的服务器服务器(仅在进行后端更改时,前端更改不会触发服务器重启)。

react-scripts start sets up the development environment and starts a server along with hot module reloading. react-scripts start设置开发环境并启动服务器以及热模块重新加载。 Basically, create-react-app helps you kick off projects without configuring, so you do not have to setup your project by yourself. 基本上,create-react-app可以帮助您启动项目而无需进行配置,因此您不必自己设置项目。

Run react-scripts build to build your project files into public folder & then, you can create a server.js file that uses Express to serve your public folder. 运行react-scripts build将您的项目文件react-scripts build到公用文件夹中,然后,您可以创建一个使用Express服务您的公用文件夹的server.js文件。

server.js server.js

const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname, 'public');

app.use(express.static(publicPath));

app.get('*', (req, res) => {
  res.sendFile(path.join(publicPath, 'index.html'));
});

app.listen(3000, () => {
  console.log('Server is up!');
});

run node server.js to start your server. 运行node server.js以启动服务器。

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

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