简体   繁体   English

简单的Todo应用在Heroku上崩溃(本地正常)

[英]Simple todo app crashed on Heroku (local works fine)

I downloaded following source, run it locally and works fine https://vuejsexamples.com/weekly-todo-list-in-vue-js/ 我下载了以下源代码,在本地运行并可以正常运行https://vuejsexamples.com/weekly-todo-list-in-vue-js/

Uploading (via Github) works fine, however when I load the page it says Application error. 上传(通过Github)工作正常,但是,当我加载页面时,显示应用程序错误。

Heroku gives me the following error. Heroku给我以下错误。 I do not even know how to access the following log file. 我什至不知道如何访问以下日志文​​件。 Any one knows? 有谁知道?

2018-08-25T20:14:40.966872+00:00 app[web.1]: npm ERR! 2018-08-25T20:14:40.966872 + 00:00 app [web.1]:npm错误!
/app/.npm/_logs/2018-08-25T20_14_40_957Z-debug.log 2018-08-25T20:14:41.322064+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=xxx-app.herokuapp.com request_id=aa2b7d48-198f-4fd1-b83e-70c92fc06ac6 fwd="84.82.96.205" dyno= connect= service= status=503 bytes= protocol=https /app/.npm/_logs/2018-08-25T20_14_40_957Z-debug.log 2018-08-25T20:14:41.322064 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”方法= GET路径=“ /” host = xxx-app.herokuapp.com request_id = aa2b7d48-198f-4fd1-b83e-70c92fc06ac6 fwd =“ 84.82.96.205” dyno = connect = service = status = 503字节=协议= https

Heroku requires that you set up an app server (HTTP server) to serve you static files. Heroku要求您设置一个应用服务器(HTTP服务器)来为您提供静态文件。 weekly-todo-vue doesn't do this. 每周待办事项不执行此操作。

I can offer 3 solutions: add a web server and change build process, extract the app build and deploy it separately with a server, use webpack-dev-server as your server on Heroku. 我可以提供3种解决方案:添加Web服务器并更改构建过程,提取应用程序构建并将其与服务器分开部署,将webpack-dev-server用作Heroku上的服务器。

Add a server and tweak the build process 添加服务器并调整构建过程

Move build into postinstall script 将构建移入安装后脚本

// package.json
scripts: {
  // omited ...
  "postinstall": "yarn run build",
  // omitted...
},

Change start npm script 更改启动 npm脚本

// package.json
scripts: {
  // omited ...
  "start": "node server.js",
  // omitted...
},

Some packages should be moved from devDependencies to dependencies since we going to build an app on Heroku and Heroku did this in the production environment. 由于我们要在Heroku上构建应用程序,而Heroku在生产环境中做到了这一点,因此应将某些软件包从devDependencies移至依赖项 This is quite tedious so you can move devDependencies into dependencies. 这非常繁琐,因此您可以将devDependencies移入依赖项。 But it will add unnecessary deps. 但这会增加不必要的深度。 into your build. 进入您的构建。

Install Express.JS dependency: 安装Express.JS依赖项:

$ yarn add express

In the root of the repo create server.js file with the app server code that will serve the app static files: 在repo的根目录中,创建带有将用于应用静态文件的应用服务器代码的server.js文件:

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

app.use(express.static(__dirname + '/dist'));

const port = process.env.PORT || 5000;

app.listen(port);

Commit all the changes and push it to Heroku 提交所有更改并将其推送到Heroku

$ git push heroku master

Extract build 提取构建

Make build from the repo: 从仓库中构建:

$ yarn run build

Create new repo and copy the dist directory. 创建新的仓库并复制dist目录。 Don't forgot to add node_modules/ into .gitignore. 不要忘记将node_modules/添加到.gitignore中。

Init new package.json: 初始化新的package.json:

$ npm init -y

Add start npm script; 添加启动npm脚本;

// package.json
scripts: {
  "start": "node server.js",
  // omitted...
},

Install Express.JS dependency: 安装Express.JS依赖项:

$ yarn add express

In the root of the repo create server.js file with the app server code that will serve the app static files, same server as in previous solution: 在repo创建server.js文件的根目录中,其中包含将为应用程序静态文件提供服务的应用程序服务器代码,与先前解决方案中的服务器相同:

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

app.use(express.static(__dirname + '/dist'));

const port = process.env.PORT || 5000;

app.listen(port);

You should have the project structure like this: 您应该具有以下项目结构:

.
├── dist
│   ├── index.html
│   └── static
│       ├── css
│       │   ├── app.c0fc4ae18c0c95b09c1cb7ceb99d491f.css
│       │   └── app.c0fc4ae18c0c95b09c1cb7ceb99d491f.css.map
│       ├── fonts
│       │   ├── icons.674f50d.eot
│       │   ├── icons.af7ae50.woff2
│       │   ├── icons.b06871f.ttf
│       │   └── icons.fee66e7.woff
│       ├── img
│       │   ├── flags.9c74e17.png
│       │   └── icons.912ec66.svg
│       └── js
│           ├── app.88670f817a4b11e940e6.js
│           ├── app.88670f817a4b11e940e6.js.map
│           ├── manifest.304e67c5c14ed63ee160.js
│           ├── manifest.304e67c5c14ed63ee160.js.map
│           ├── vendor.434b5723496264d2da17.js
│           └── vendor.434b5723496264d2da17.js.mapo
├── package-lock.json
├── package.json
└── server.js

Commit the code and push it to Heroku. 提交代码并将其推送到Heroku。

Use webpack-dev-server 使用webpack-dev-server

As stated here you can use webpack-dev-server to serve your app. 如此处所述您可以使用webpack-dev-server服务您的应用程序。 But this is highly not recommended for production applications. 但是,强烈建议不要将其用于生产应用程序。

Add webpack dev server config to webpack.prod.conf.js: 将webpack开发服务器配置添加到webpack.prod.conf.js:

     // ..omitted
     devtool: config.build.productionSourceMap ? config.build.devtool : false,
  +  devServer: {
  +    disableHostCheck: true,
  +    clientLogLevel: 'warning',
  +    historyApiFallback: true,
  +    hot: false,
  +    host: '0.0.0.0',
  +    port: process.env.PORT || config.dev.port,
  +    open: false,
  +    overlay: false,
  +    publicPath: config.dev.assetsPublicPath,
  +    proxy: config.dev.proxyTable,
  +    quiet: true // necessary for FriendlyErrorsPlugin
  +  },
     output: {
     // ..omitted

Change start npm script: 更改启动npm脚本:

// package.json
scripts: {
  // omited ...
  "start": "webpack-dev-server --port $PORT --host 0.0.0.0 --config build/webpack.prod.conf.js",
  // omitted...
},

Some packages should be moved from devDependencies to dependencies since we going to build an app on Heroku and Heroku did this in the production environment. 由于我们要在Heroku上构建应用程序,而Heroku在生产环境中做到了这一点,因此应将某些软件包从devDependencies移至依赖项 This is quite tedious so you can move devDependencies into dependencies. 这非常繁琐,因此您可以将devDependencies移入依赖项。 But it will add unnecessary deps. 但这会增加不必要的深度。 into your build. 进入您的构建。

Commit the change and push to Heroku. 提交更改并推送到Heroku。

Node/npm version issue with Heroku Heroku的节点/ npm版本问题

Heroku loves strict engine specs in package.json. Heroku喜欢package.json中严格的引擎规格。 I would recommend that you specify explicit versions of Node.JS and NPM: 我建议您指定Node.JS和NPM的显式版本:

   "engines": {
-    "node": ">= 8.0.0",
-    "npm": ">= 5.0.0"
+    "node": "8.11.4",
+    "npm": "5.6.0"
   },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM