[英]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: 遇到类似的难题,这就是我所做的:
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.