2017-05-22 50 views
2

我想建立一個應用程序,我又把它部署到Heroku上,但在瀏覽器中我得到的錯誤:的NodeJS項目成功部署到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 [email protected] 
2017-05-22T03:58:35.925318+00:00 app[api]: Deploy 53f249eb by user [email protected] 
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:網絡:節點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日誌不顯示有用的錯誤消息。

+0

嘗試在本地運行你的服務器:使用命令'heroku run local'。看起來你的服務器有一些錯誤。 – chenkehxx

+0

運行命令'heroku run local'我得到錯誤:「bash:line 0:local:只能在函數中使用」。我試着用'heroku local web'運行,我得到 '[WARN]無法讀取屬性'null' [OKAY]發現package.json文件 - 嘗試'npm start' [WARN]找不到ENV文件 [WARN]無法讀取null的屬性'1' [OKAY]發現package.json文件 - 嘗試'npm start'' 我可以從localhost:5000訪問我的應用程序。 – max

+0

所以它意味着你的'package.json'有問題,你需要仔細檢查它。或者您可以嘗試運行您在'Procfile'中定義的命令。 – chenkehxx

回答

0

您的代碼無法讀取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) 
+0

我檢查了我的heroku bash,數據/ customers.json和data/states.json已經部署到了heroku服務器。 – max

+0

你爲'PORT'環境變量設置了什麼?如果你沒有設置它,你有權訪問5000端口嗎? – sabrehagen

+0

我沒有設置它。我認爲它會根據heroku服務器自動分配?如果我在heroku bash中手動運行節點server.js,它會打印「節點應用程序在端口26597上運行」。 – max

相關問題