Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在同一端口上运行前端和后端以部署在tomcat服务器中?_Mysql_Node.js_Reactjs_Tomcat - Fatal编程技术网

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

Mysql 如何在同一端口上运行前端和后端以部署在tomcat服务器中?,mysql,node.js,reactjs,tomcat,Mysql,Node.js,Reactjs,Tomcat,我在前端使用reactjs,在后端使用node js和express js,后者连接到MySQL数据库 这两个都是两个不同的文件夹,通常我在不同的端口上使用它们 如何将它们结合起来部署在tomcat服务器中。正确的方法是什么 这是我的前端文件夹中后端文件夹的索引文件 .... .... app.post('/data/tournament/registration',(request, response)=>{ console.log(request.body); conn

我在前端使用reactjs,在后端使用node js和express js,后者连接到MySQL数据库

这两个都是两个不同的文件夹,通常我在不同的端口上使用它们

如何将它们结合起来部署在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");
});
....
....
{
  "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"
  }
}

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

....
....
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");
});
....
....
{
  "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'
});
....
....
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));
};
....
....
我在前端使用axios实例从后端获取数据

import axios from 'axios';

export default axios.create({
    baseURL:'http://localhost:8080'
});
....
....
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"
    ]
  }
}


更新

至于我们在评论中讨论的关于将xml文件等放在何处的另一个问题,您将希望在web.xml文件中使用它来处理任何404


请发布一些你已经尝试过的代码。然后我可以回答你的问题哪个包json文件,来自服务器文件夹还是后端文件夹?我的前端文件夹中有后端文件夹,这很好。代理告诉React在哪里找到服务器,因此您希望它位于client(React)package.json中。更多信息-->我正在尝试将整个项目文件夹转换为war文件,用于在tomcat服务器中部署,请帮助我。我使用mvn package命令将其转换为war文件,其中我将pom.xml和web.xml文件放在根文件夹中,将web.xml放在根文件夹中,并包含
错误页
标记,以便重定向任何404。(见我的最新答案)。。此外,此链接可能有助于。。。我看过那篇文章,但他只解释了前端的部署,而没有解释后端的部署——请你在zoom会话中给我解释一下,兄弟。这对我来说真的很重要。