简体   繁体   English

我可以在不运行“npm run build”的情况下从快速服务器提供 create react 应用程序吗?

[英]Can I serve a create react app from an express server without running 'npm run build'?

I have a nodejs with express server.我有一个带有快速服务器的 nodejs。 I would like to server a create react app from the server.我想从服务器提供一个 create react 应用程序。 Is this possible with out building the react app?如果不构建 React 应用程序,这可能吗?

Example from a project im working on right now.我现在正在做的一个项目的例子。

I server a built react app like this...我为这样的内置反应应用程序提供服务...

 //// Server static resources.
ROUTER.use('/static', EXPRESS.static(PATH.join(__dirname, '../', '../', 'project', 'build', 'static')));
//// Server built react app. 
ROUTER.get('/',  function(req, res) {        
    res.sendFile(  PATH.join(__dirname, '../', '../', 'project', 'build', 'index.html' ));
});

Any way I can server the react app without building it?有什么方法可以在不构建 React 应用程序的情况下为其提供服务? Something like...就像是...

ROUTER.get('/', function(req, res) {
    //redirect to loopback port for react app. 
});

You need to define a project layout which suits you well.您需要定义一个适合您的项目布局

Since you have client side and server side code, you could maintain two folders, each one containing module-specific code.由于您有客户端和服务器端代码,您可以维护两个文件夹,每个文件夹都包含特定于模块的代码。

CLI tools like create-react-app expects such approach to be adopted, unless your project uses some kind of SSRcreate-react-app这样的 CLI 工具期望采用这种方法,除非您的项目使用某种SSR

A reasonable good project layout looks like this:一个合理的好的项目布局如下所示:

my-project/
├── my-project-react/
│   ├── package.json 
│   ├── src/  
│   ├── (... other project files) 
│   └── README.md 
├── my-project-express/
│   ├── package.json 
│   ├── app/ 
│   ├── (... other project files) 
│   └── README.md 
├── .gitignore 
└── README.md

That way you can serve express with nodemon and react with webpack dev server, just remember to enable cors on express .这样你就可以用 nodemon 服务 express 并与 webpack dev server 反应,只记得在express 上启用 cors You also must adjust the service address inside your web app project, depending on NODE_ENV.您还必须根据 NODE_ENV 调整 Web 应用项目内的服务地址。

crisp-react has a backend subproject based on Express that serves a sample React application (which is not CRA). crisp-react有一个基于 Express 的后端子项目,它为示例 React 应用程序(不是 CRA)提供服务。

If you follow Getting started or alternatively execute commands:如果您按照入门或执行命令:

  • git clone https://github.com/winwiz1/crisp-react.git
  • cd crisp-react
  • yarn install && yarn start:prod
    Express is running...快递正在运行...
    Terminate by Control+CControl+C终止

then you will have a running instance of Express serving a react app which you can see by pointing a browser to localhost:3000.然后您将有一个运行的 Express 实例,提供一个 React 应用程序,您可以通过将浏览器指向 localhost:3000 来查看该实例。 The backend/Express subproject only serves the React app, it doesn't build the app and in fact cannot build it because the backend doesn't have React library in its package.json as a dependency. backend/Express 子项目只服务于 React 应用程序,它不构建应用程序,实际上无法构建它,因为后端在其package.json 中没有作为依赖项的 React 库。

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

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