NodeJS项目成功部署到Heroku但App崩溃了

时间:2017-05-22 04:28:10

标签: node.js express heroku

我正在尝试构建一个App并将其部署到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日志未显示有用的错误消息。

1 个答案:

答案 0 :(得分: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)