以下设置:
- Node Express 服务器,在 localhost:5000 上运行。
- 直接在服务器文件夹中使用
create-react-app
创建的客户端文件夹。 - CI 与 Travis CI 集成
我想在应用程序通过时将其部署到heroku。因此我创建了以下 .travis.yml
:
language: node_js
before_install:
- npm install && node index.js &
before_script: cd ./client && npm install
node_js:
- "stable"
cache:
directories:
- node_modules
script:
- npm run test
- npm run lint
- npm run build
notifications:
slack: clicker-web:myslack
deploy:
provider: heroku
api_key: "mykey"
app: test999111test
on: heroku-deployment-testing
所以我把它部署到了heroku,并且没有因travis而失败。 但在 heroku 应用程序本身中,我只是收到错误(在控制台中):
npm ERR! code ELIFECYCLE
2018-04-15T15:15:31.033343+00:00 app[web.1]: npm ERR! errno 1
2018-04-15T15:15:31.034412+00:00 app[web.1]: npm ERR! Clicker@0.1.0 start: `node scripts/start.js`
2018-04-15T15:15:31.034622+00:00 app[web.1]: npm ERR! Exit status 1
2018-04-15T15:15:31.034841+00:00 app[web.1]: npm ERR!
2018-04-15T15:15:31.035019+00:00 app[web.1]: npm ERR! Failed at the Clicker@0.1.0 start script.
所以我很确定我的设置一定有问题。
这是我在服务器(根)目录中设置的package.json
(只是您需要的代码片段):
"engines": {
"node": "8.1.1",
"npm": "5.0.3"
},
"scripts": {
"client": "npm start --prefix client",
"buildclient": "npm build --prefix client",
"server": "nodemon index.js",
"dev":
"concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
"build": "concurrently \"npm run server\" \"npm run client\"",
"test": "npm run server",
"start": "node index.js",
"heroku-postbuild":
"npm run install --prefix client && npm run build --prefix client"
我在我的应用程序中使用代理进行开发,效果很好。但在 heroku 的产品中我不需要它。我在 index.js
(在服务器根目录中)创建了这些行:
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
但我总是从 Heroku 得到同样的错误。我做错了什么以及如何使用最佳实践将服务器+客户端组合的项目部署到 Travis 的 Heroku 上?
这是项目,您可以在其中看到文件结构。我现在在 heroku-deployment-testing
分支。
这里还有 herokuapp,除了错误之外没有显示任何内容:
最后但并非最不重要的 travis 日志(部署的最后一部分):
最佳答案
尝试使用此配置
language: node_js
node_js:
- '8'
cache:
directories:
- node_modules
- client/node_modules
install:
- npm install
- npm run build
script:
- nohup npm run start &
- sleep 3
- npm run test
- npm run lint
deploy:
provider: heroku
api_key:
secure: API_KEY
app: YOUR_APP_NAME
我相信您不需要 before_install
和 before_script
部分。
而且您也不需要运行 npm run build
脚本,因为您是在 package.json 中使用“heroku_postbuild”脚本构建应用程序。
关于node.js - 使用 Travis CI 运行后,如何将 Node Express 和 React 应用程序部署到 Heroku?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843630/