【问题标题】:How to host nodeJS server and react app on heroku如何在heroku上托管nodeJS服务器和反应应用程序
【发布时间】:2019-09-09 07:47:22
【问题描述】:

我已经上传了服务端和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 中。

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

【问题讨论】:

  • 您的服务器正在监听哪个端口?
  • 我当前的端口实现是var port = normalizePort(process.env.PORT || '3001');

标签: node.js reactjs heroku


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2021-06-30
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多