简体   繁体   English

如何在Tomcat中部署React应用程序?

[英]How deploy React app in Tomcat?

Problem Statement : I have two parts of my application. 问题陈述 :我的申请分为两部分。 front-end and back-end. 前端和后端。 front-end is built using React which uses Node.js and back-end is maven web app, and the communication between front-end and back-end is done using REST . 前端是使用React使用Node.js构建的,后端是maven Web应用,前端和后端之间的通信是使用REST完成的。 my application is running on two server. 我的应用程序在两台服务器上运行。 front-end is running on Node.js ans back-end is running on tomcat. 前端在Node.js上运行,后端在tomcat上运行。 What I want is to deploy both the part on the same server. 我要在同一服务器上部署这两个部分。

How do I do this? 我该怎么做呢?

What i tried. 我试过了。

1st approach : I tried using maven plugin. 第一种方法 :我尝试使用Maven插件。 added the bellow plugin in pom.xml pom.xml添加了波纹管插件

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <id>exec-npm-install</id> <phase>compile</phase> <configuration> <executable>npm</executable> <arguments> <argument>--prefix</argument> <argument>${basedir}/src/main/webapp/ui</argument> <argument>install</argument> <argument>${basedir}/src/main/webapp/ui</argument> </arguments> </configuration> <goals> <goal>exec</goal> </goals> </execution> <execution> <id>exec-webpack</id> <phase>compile</phase> <configuration> <executable>npm</executable> <arguments> <argument>--prefix</argument> <argument>${basedir}/src/main/webapp/ui</argument> <argument>run</argument> <argument>build</argument> </arguments> </configuration> <goals> <goal>exec</goal> </goals> </execution> </executions> </plugin> 

Added the front-end application folder in back-end, web-app folder. 在后端Web应用程序文件夹中添加了前端应用程序文件夹。 but the problem with this approach is, when I do mvn install all node module are downloaded and the size of the project become very large( 700MB ) because of the node module. 但是这种方法的问题是,当我执行mvn install所有node module都被下载,并且由于node module ,项目的大小变得非常大( 700MB )。 This is not desire. 这不是欲望。

How do I do that? 我怎么做?

Node.js is by itself a server, and cannot be handled by tomcat. Node.js本身就是一台服务器,不能由tomcat处理。 You have to install both your servers applications (front-end on node & back-end on tomcat) side by side. 您必须同时安装两个服务器应用程序(节点上的前端和tomcat上的后端)。

As your front-end files will not be served by tomcat, you should not install them within tomcat folders and keep them separate (you can still use maven to install them though). 由于您的前端文件不会由tomcat提供,因此您不应将它们安装在tomcat文件夹中并将它们分开(尽管您仍然可以使用maven来安装它们)。

Dependencies files (node_module folder) are needed by the node.js server, you cannot get rid of it. node.js服务器需要依赖文件(node_module文件夹),您无法摆脱它。

Also covered in Can node js be run within tomcat server 可以在tomcat服务器中运行Node js吗

Ran into similar puzzle, and this is what I did: 遇到类似的难题,这就是我所做的: 在此处输入图片说明

  1. Created my Webpack project in the root of my whole project, that way I can keep the node_modules folder out of FrontEnd, and still tell Webpack to put the build stuff inside FrontEnd, where my Tomcat application can pick up the index.jsp. 在我的整个项目的根目录中创建了Webpack项目,这样我就可以将node_modules文件夹保留在FrontEnd之外,并且仍然告诉Webpack将构建内容放入FrontEnd中,我的Tomcat应用程序可以在其中将index.jsp拾取。 (The output folder for this has to me inside the Webpack application, that is what I had to put it in the top root, even if it does not look very organized) (为此,输出文件夹必须位于Webpack应用程序内部,这是我必须将其放在最高根目录中的内容,即使它看起来不太井井有条)

Here is the configuration for output in webpack.production.config: 这是webpack.production.config中输出的配置:

  output: {
    filename: '[name].[contenthash].js',
    path: path.resolve(__dirname, 'FrontEnd/src/main/webapp/'), 
    publicPath: '' 
  },

As a result, when you run npm run build in your console, webpack generates the output files and place it inside the FrontEnd: 结果,当您在控制台中运行npm run build时,webpack会生成输出文件并将其放置在FrontEnd中: 在此处输入图片说明

As last thing, to tell Webpack to generate an JSP file, and also to tell it to clean everything except WEB-INF and META-INF in every build, you will need to add these pluggings to WebPack: 最后,要告诉Webpack生成JSP文件,并告诉它在每个构建中清除除WEB-INF和META-INF以外的所有内容,您需要将以下插件添加到WebPack中:

  plugins: [ 
    new CleanWebpackPlugin(['AMS/src/main/webapp/'], {
        exclude: ['WEB-INF', 'META-INF'],
        verbose: true
    }),
    new HtmlWebpackPlugin({ 
      filename:'index.jsp' 
    })
  ]

I hope it helps.. Good Luck!! 希望对您有所帮助。

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

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