如何在Heroku上托管NodeJS服务器并做出反应

时间:2019-07-08 08:43:48

标签: node.js reactjs heroku

我已经上传了服务器和react客户端,但是服务器界面是显示的界面,而不是react界面。

我尝试将服务器的package.json文件中的“脚本”部分混在一起,但这似乎无济于事。

这是服务器的package.json的样子:

{
  "name": "application",
  "version": "0.0.0",
  "private": true,
  "engines": {
    "node": "9.11.2",
    "npm": "6.x"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "csv-parser": "^2.3.0",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "express-mysql-session": "^2.1.0",
    "express-session": "^1.16.2",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "md5": "^2.2.1",
    "morgan": "~1.9.1",
    "multer": "^1.4.1",
    "mysql": "^2.17.1",
    "node-stringify": "^0.2.1",
    "xmlbuilder": "^13.0.2"
  },
  "devDependencies": {
    "nodemon": "^1.19.1"
  },
  "scripts": {
    "start": "node ./bin/www",
    "client": "cd frontend && npm start",
    "heroku-postbuild": "cd frontend && npm install && npm run build"
  }
}

这是客户端(在本例中为前端)的package.json:

{
  "name": "app-frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@navjobs/upload": "^3.1.3",
    "async-validator": "^1.8.2",
    "axios": "^0.17.0",
    "canvas2pdf": "^1.0.5",
    "express": "^4.16.3",
    "file-exists": "^5.0.1",
    "fs2": "^0.3.4",
    "howler": "^2.0.5",
    "html2canvas": "^1.0.0-alpha.12",
    "js-file-download": "^0.4.7",
    "jspdf": "^1.3.5",
    "moment": "^2.22.1",
    "paginate-array": "^2.0.0",
    "path-exists": "^4.0.0",
    "react": "^16.3.1",
    "react-d3-tree": "^1.13.0",
    "react-dom": "^16.2.0",
    "react-google-login": "^3.0.10",
    "react-helmet": "^5.2.0",
    "react-html-parser": "^2.0.2",
    "react-native": "^0.55.2",
    "react-native-fs": "^2.13.3",
    "react-pagination-table": "^2.0.2",
    "react-redux": "^5.0.6",
    "react-reveal": "^1.2.1",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^5.0.0-alpha.9",
    "react-scripts": "^2.1.0",
    "react-style-tag": "^2.0.1",
    "react-toastify": "^4.0.0-rc.4",
    "reactable": "^1.1.0",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-pack": "^0.1.5",
    "redux-thunk": "^2.2.0",
    "semantic-ui-css": "^2.2.14",
    "semantic-ui-react": "^0.79.1",
    "shape-json": "^1.2.4",
    "styled-components": "^4.3.2",
    "underscore": "^1.9.0",
    "url-exists": "^1.0.3",
    "vis": "^4.21.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "precommit": "lint-staged"
  },
  "devDependencies": {
    "husky": "^0.14.3",
    "lint-staged": "^4.2.3",
    "prettier": "^1.7.2"
  },
  "lint-staged": {
    "*.js": [
      "prettier --tab-width 2 --single-quote --jsx-bracket-same-line --write",
      "git add"
    ]
  },
  "proxy": "http://localhost:3001"
}

这是我的文件结构:

.
└── application
    ├── app.js
    ├── assets
    ├── bin
    ├── controllers
    ├── frontend
    │   ├── index.js
    │   ├── package.json (this is the package.json for the frontend)
    │   ├── public
    │   ├── README.md
    │   └── src
    ├── models
    ├── node_modules
    ├── package.json (this is the package.json for the server)
    ├── package-lock.json
    ├── public
    ├── routes
    └── views

为了以防万一,我的服务器端口侦听器位于/bin/www下。我定义了一个端口,然后创建一个server = createHttpServer(),其中服务器变量是侦听该端口的变量,并将其导入到app.js中。

万一有人知道如何解决我的问题,请帮助我。我想测试在线部署我的应用程序。

1 个答案:

答案 0 :(得分:0)

当您使用create-react-app时,这可能是自动git init的问题-您的前端文件夹可能有自己的git文件夹。这会导致我称之为gitception的问题。当您进行git init并从根目录创建heroku时-您将在根目录中拥有另一个git文件夹。当您推送到heroku时-它不包括前端文件夹b / c,它是一个单独的git实例。您可以通过在编辑器中打开项目并可视化隐藏文件来更正此问题,以便查看git文件夹。将前端git文件夹移至垃圾箱。现在,您需要从缓存中删除前端git实例:git rm -f --cached frontend && git add。 && git commit -m'将前端文件夹添加回git repo'&& git push heroku master