如何在同一端口中运行前端和后端以部署在tomcat服务器中?

时间:2020-05-27 04:22:03

标签: mysql node.js reactjs tomcat

我在前端使用reactjs,在节点js使用后端,在连接到MySQL DB的后端使用js。

这是两个我通常在不同端口上使用的文件夹。

如何将它们组合以部署在tomcat服务器中。正确的方法是什么?

这是前端文件夹中后端文件夹中的索引文件

....
....
app.post('/data/tournament/registration',(request, response)=>{
    console.log(request.body);
    connection.query("INSERT INTO registrationdata VALUES ?"
    ,[request.body],function(error, result, fields){
        if(error){
            throw error;
        }else{
            console.log(JSON.stringify(result));
            response.send(JSON.stringify(result))
        }
    });
});

app.listen(8080,()=>{
    console.log("Connected to port 8080");
});
....
....

前端文件夹内后端文件夹中的软件包JSON文件

{
  "name": "mysql_db",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node src/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mysql": "^2.18.1"
  }
}

我在前端使用axios从后端获取数据。

import axios from 'axios';

export default axios.create({
    baseURL:'http://localhost:8080'
});

我在前端使用axios实例从后端获取。

....
....
export const fetchTournaments=()=>async(dispatch)=>{
    await mysqlDB.get('/data/tournament/fetch')
    .then((response)=>{
        console.log(response);
        if(response.data !== {}){
            dispatch( {
                type: ActionTypes.FETCH_TOURNAMENT_SUCCESS,
                payload:response.data 
            });
            console.log("Fetched tournament")
            History.push('/tournaments')
        }else{
            dispatch({type:ActionTypes.FETCH_TOURNAMENT_FAILED});
            console.log("failed to Fetch tournament")

        }
    })
    .catch((error)=>console.error(error));
};
....
....

这是前端的json包文件,基本上是根文件夹

{
  "name": "chess4loudoun",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@date-io/moment": "^1.3.13",
    "@material-ui/core": "^4.9.14",
    "@material-ui/icons": "^4.9.1",
    "@material-ui/pickers": "^3.2.10",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "concurrently": "^5.2.0",
    "material-table": "^1.57.2",
    "moment": "^2.26.0",
    "react": "^16.13.1",
    "react-countdown": "^2.2.1",
    "react-dom": "^16.13.1",
    "react-google-login": "^5.1.20",
    "react-material-ui-carousel": "^1.4.5",
    "react-redux": "^7.2.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.1",
    "redux": "^4.0.5",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start ",
    "build": "react-scripts build ",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
  },

  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

更新

关于您在评论中讨论过的关于将xml文件等放置在何处的其他问题,您将需要在web.xml文件中使用它来处理根据https://create-react-app.dev/docs/advanced-configuration/

的任何404。
<error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
</error-page>

对于开发,您可以使用:

将此添加到您的express server.js或w / e

app.listen(8080)

然后在您的React App的package.json中使用:

"proxy": "http://localhost:8080"

在生产中,您将需要使用path模块来提供静态反应文件...

const express = require('express')
const path = require('path')
const app = express()

app.use(express.static(path.join(__dirname, 'build')))