【问题标题】:NodeJS project successfully deploy to Heroku but App CrashedNodeJS 项目成功部署到 Heroku 但应用程序崩溃
【发布时间】:2017-10-21 15:04:31
【问题描述】:

我正在尝试构建一个应用程序并将其部署到 heroku,但在浏览器中出现错误:

Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.

在 heroku 日志中我得到:

2017-05-22T03:58:35.925318+00:00 app[api]: Release v19 created by user xxx@gmail.com
2017-05-22T03:58:35.925318+00:00 app[api]: Deploy 53f249eb by user xxx@gmail.com
2017-05-22T03:58:08.000000+00:00 app[api]: Build succeeded
2017-05-22T03:58:36.628543+00:00 heroku[web.1]: State changed from crashed to starting
2017-05-22T03:58:42.060016+00:00 heroku[web.1]: Starting process with command `: node server.js`
2017-05-22T03:58:44.343952+00:00 heroku[web.1]: State changed from starting to crashed
2017-05-22T03:58:44.330459+00:00 heroku[web.1]: Process exited with status 0
2017-05-22T03:58:45.036725+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myappfrontend.herokuapp.com request_id=c13cbc90-b663-4202-bf23-fdd7839bd11a fwd="124.13.47.105" dyno= connect= service= status=503 bytes= protocol=https
2017-05-22T03:58:46.355457+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myappfrontend.herokuapp.com request_id=10d85673-140b-4510-a5cb-b6b3db20e1a7 fwd="124.13.47.105" dyno= connect= service= status=503 bytes= protocol=https

部署时我得到:

remote: Building source:
remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=false
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  7.10.0
remote:        engines.npm (package.json):   4.2.0
remote:        
remote:        Downloading and installing node 7.10.0...
remote:        npm 4.2.0 already installed with node
remote: 
remote: -----> Restoring cache
remote:        Loading 2 from cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (not cached - skipping)
remote: 
remote: -----> Building dependencies
remote:        Installing node modules (package.json)
remote: 
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (nothing to cache)
remote: 
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 47.1M
remote: -----> Launching...
remote:        Released v20
remote:        https://myappfrontend.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.

我有一个 Procfile : web : node server.js

我的 server.js :

"use strict";
var express     = require('express'),
    bodyParser  = require('body-parser'),
    fs          = require('fs'),
    app         = express(),
    customers   = JSON.parse(fs.readFileSync('data/customers.json', 'utf-8')),
    states      = JSON.parse(fs.readFileSync('data/states.json', 'utf-8'));

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

//Would normally copy necessary scripts into src folder (via grunt/gulp) but serving
//node_modules directly to keep everything as simple as possible
app.use('/node_modules', express.static(__dirname + '/node_modules')); 

//The src folder has our static resources (index.html, css, images)
app.use(express.static(__dirname + '/src')); 


app.post('/api/auth/login', (req, res) => {
    var userLogin = req.body;
    //Add "real" auth here. Simulating it by returning a simple boolean.
    res.json(true);
});

app.post('/api/auth/logout', (req, res) => {
    res.json(true);
});

// redirect all others to the index (HTML5 history)
app.all('/*', function(req, res) {

    res.sendFile(__dirname + '/src/index.html');
});
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

最后是我的 package.json:

{
    "name": "angular-jumpstart",
    "author": "Dan Wahlin",
    "version": "2.0.0",
    "repository": "https://github.com/danwahlin/angular-jumpstart",
    "scripts": {
        "clean": "del-cli \"src/app/**/*.js\" \"src/app/**/*.js.map\" \"src/devDist\" \"src/dist\"",
        "build": "npm run clean && webpack --progress --watch",
        "tsc": "tsc",
        "tsc:w": "tsc -w",
        "start:nodemon": "tsc && concurrently \"tsc -w\" \"nodemon server.js\" ",
        "start": "tsc && concurrently \"tsc -w\" \"node server.js\" "
    },
    "license": "ISC",
    "dependencies": {
        "@angular/common": "4.0.0",
        "@angular/compiler": "4.0.0",
        "@angular/compiler-cli": "4.0.0",
        "@angular/core": "4.0.0",
        "@angular/forms": "4.0.0",
        "@angular/http": "4.0.0",
        "@angular/platform-browser": "4.0.0",
        "@angular/platform-browser-dynamic": "4.0.0",
        "@angular/router": "4.0.0",
        "@angular/upgrade": "4.0.0",
        "@angular/platform-server": "4.0.0",
        "@angular/tsc-wrapped": "4.0.0",
        "@angular/animations": "4.0.0",
        "systemjs": "0.19.47",
        "core-js": "2.4.1",
        "rxjs": "5.2.0",
        "zone.js": "0.8.5",
        "express": "4.15.2",
        "body-parser": "1.17.1"
    },
    "devDependencies": {
        "@types/node": "7.0.11",
        "@types/google-maps": "3.2.0",
        "concurrently": "3.4.0",
        "lite-server": "2.3.0",
        "typescript": "2.2.1",
        "opn": "4.0.2",
        "del-cli": "0.2.1",
        "webpack": "2.3.2",
        "html-webpack-plugin": "2.28.0",
        "webpack-merge": "4.1.0",
        "extract-text-webpack-plugin": "2.1.0",
        "angular2-template-loader": "0.6.2",
        "angular-router-loader": "0.5.0",
        "awesome-typescript-loader": "3.1.2",
        "css-loader": "0.27.3",
        "to-string-loader": "1.1.5",
        "raw-loader": "0.5.1",
        "style-loader": "0.16.0",
        "@ngtools/webpack": "1.3.0"
    },
      "engines": {
    "node": "7.10.0",
    "npm": "4.2.0"
  }
}

知道为什么它不起作用吗? Heroku 日志没有显示有用的错误消息。

【问题讨论】:

  • 尝试在本地运行您的服务器:使用命令heroku run local。您的服务器似乎有一些错误。
  • 运行命令heroku run local 我得到错误:“bash: line 0: local: can only be used in a function”。我尝试使用heroku local web 运行,我得到[WARN] Cannot read property '1' of null [OKAY] package.json file found - trying 'npm start' [WARN] No ENV file found [WARN] Cannot read property '1' of null [OKAY] package.json file found - trying 'npm start' 我可以从localhost:5000 访问我的应用程序。
  • 所以说明你的package.json有问题,需要仔细检查。或者你可以尝试运行你在Procfile中定义的命令。

标签: node.js express heroku


【解决方案1】:

您的代码无法读取data/customers.json 文件,因为该文件不存在。一旦你将此文件连同你的代码一起上传到 Heroku,你的服务器就会运行。

fs.js:583
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'data/customers.json'
    at Object.fs.openSync (fs.js:583:18)
    at Object.fs.readFileSync (fs.js:490:33)
    at Object.<anonymous> (/tmp/test.js:6:33)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)

【讨论】:

  • 我签入了我的 heroku bash,data/customers.json 和 data/states.json 都已部署到 heroku 服务器中。
  • 您为PORT 环境变量设置了什么?如果不设置,是否可以访问 5000 端口?
  • 我没有设置。我以为它会根据heroku服务器自动分配?如果我在 heroku bash 中手动运行 node server.js,它会打印“Node app is running on port 26597”。
猜你喜欢
  • 2020-08-06
  • 2015-09-22
  • 1970-01-01
  • 2017-08-27
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 2015-10-11
相关资源
最近更新 更多