首页 > 解决方案 > 如何将 NodeJS 应用程序转换为 war 文件

问题描述

我创建了一个仪表板,前端使用 React JS,后端使用 Node JS。我已经为 React JS 项目构建了 build 文件夹,现在 build 文件夹位于 Node JS 应用程序中。我的意思是,当我启动 npm 时,服务器开始在 localhost 4000 上运行,并且能够从 build 文件夹访问前端的所有文件,这意味着当我运行节点 js 服务器时项目正在运行在本地系统上。问题是我想将此基于节点 JS 的应用程序转换为 war 文件,以便能够将其部署在 apache 服务器上。我对这整个部署工作很陌生,所以欢迎任何帮助。

标签: node.jsreactjsapachedeploymentwar

解决方案


node.js 意味着创建你自己的 http 服务器,运行你的 javascript 代码,也可以安装 express 模块来创建 express 服务器来运行你的 node.js 应用程序,同样有很多不同的库来创建和运行 node js应用程序通过创建服务器,tomcat 表示 java http web 服务器,即 java 兼容的运行环境。

您已尝试创建构建,您的package.json

"homepage":"http://localhost:8080/YOURAPPNAME",  
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build"    
  }

如果您使用的是 maven,则可以访问pom.xml,您可以进行配置,为 prod 配置配置文件,使用org.codehaus.mojo

<configuration>
  <environmentVariables>                                
    <PUBLIC_URL>http://frugalisminds.com/${project.artifactId}</PUBLIC_URL>
    <REACT_APP_ROUTER_BASE>/${project.artifactId}</REACT_APP_ROUTER_BASE>
  </environmentVariables>
</configuration>

你可以BrowserRouter在 app.jsx 文件中设置你的

<BrowserRouter basename={process.env.REACT_APP_ROUTER_BASE || ''}> 

您可能需要配置web.xml重定向错误到索引页面,

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          version="2.4">
  <display-name>YOUR REACT SERVLET PROJECT NAME</display-name>
  <error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
  </error-page>    
</web-app>

现在创建warmvn package,你可以探索一下webpack-war-plugin


推荐阅读