Heroku Application error: An error occurred in the application and your page could not be served

Viewed 23536

I'm building an app and I've gotten to the point where deployment to HEROKU was successful. On opening app(https://odm-mobile.herokuapp.com/), I get APPLICATION ERROR, I checked log with CLI command and this is what I get:

2018-10-22T09:06:32.000000+00:00 app[api]: Build started by user aailoje@gmail.com

2018-10-22T09:06:45.572183+00:00 app[api]: Deploy 7bc663ae by user aailoje@gmail.com

2018-10-22T09:06:45.572183+00:00 app[api]: Release v24 created by user aailoje@gmail.com

2018-10-22T09:06:45.826688+00:00 heroku[web.1]: State changed from crashed to starting

2018-10-22T09:06:47.000000+00:00 app[api]: Build succeeded

2018-10-22T09:06:49.116924+00:00 heroku[web.1]: Starting process with command `npm start`

2018-10-22T09:06:53.099327+00:00 app[web.1]:

2018-10-22T09:06:53.099345+00:00 app[web.1]: > odm-mobile@1.0.0 start /app

2018-10-22T09:06:53.099347+00:00 app[web.1]: > node index.js

2018-10-22T09:06:53.099349+00:00 app[web.1]:

2018-10-22T09:06:53.840102+00:00 heroku[web.1]: Process exited with status 0

2018-10-22T09:06:53.856482+00:00 heroku[web.1]: State changed from starting to crashed

2018-10-22T09:06:53.858384+00:00 heroku[web.1]: State changed from crashed to starting

2018-10-22T09:06:57.152906+00:00 heroku[web.1]: Starting process with command `npm start`

2018-10-22T09:07:00.298331+00:00 app[web.1]:

2018-10-22T09:07:00.298348+00:00 app[web.1]: > odm-mobile@1.0.0 start /app

2018-10-22T09:07:00.298350+00:00 app[web.1]: > node index.js

2018-10-22T09:07:00.298351+00:00 app[web.1]:

2018-10-22T09:07:00.979144+00:00 heroku[web.1]: Process exited with status 0

2018-10-22T09:07:00.997278+00:00 heroku[web.1]: State changed from starting to crashed

2018-10-22T09:07:20.183091+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=odm-mobile.herokuapp.com request_id=c9a8b164-2f61-459b-87da-56518ce4dc81 fwd="154.120.97.254" dyno= connect= service= status=503 bytes= protocol=https

2018-10-22T09:07:22.293716+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=odm-mobile.herokuapp.com request_id=6870b281-7607-4882-8bac-e97c38169e26 fwd="154.120.97.254" dyno= connect= service= status=503 bytes= protocol=https

2018-10-22T09:25:14.164205+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=odm-mobile.herokuapp.com request_id=af0d9ab3-8a64-494e-8b2e-4b5e0a46f7fd fwd="154.120.97.254" dyno= connect= service= status=503 bytes= protocol=https

2018-10-22T09:25:15.783603+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=odm-mobile.herokuapp.com request_id=dcd6cc6f-0959-4ca7-b4d4-43143e952b8c fwd="154.120.97.254" dyno= connect= service= status=503 bytes= protocol=https`

The following are the files that I'm working with on dreamweaver

  1. index.js

    var port = Number(process.env.PORT|| 5000);
    var express = require('express'),
        moment = require('moment');
    
    var exports = module.exports = function(dir, options) {
        var modules = {};
        options = merge(options || {}, {
            lazy: true
        });
    
        fs.readdirSync(dir).forEach(function(filename) {
            // filter index and dotfiles
            if (filename !== 'index.js' && filename[0] !== '.') {
                var moduleName = path.basename(filename, path.extname(filename));
                var modulePath = path.join(dir, moduleName);
                // lazy load
                if (options.lazy) {
                    Object.defineProperty(modules, moduleName, {
                        get: function() {
                            return require(modulePath);
                        }
                    });
                } else {
                    modules[moduleName] = require(modulePath);
                }
            }
        });
    
        return modules;
    };

  2. package.json

    {
      "name": "odm-mobile",
      "version": "1.0.0",
      "description": "odm daily devotional",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "keywords": [],
      "author": "xdoshent <aailoje@gmail.com>",
      "license": "MIT",
      "repository": {
        "type": "git",
        "url": "odm-mobile"
      },
      "dependencies": {
        "@fortawesome/fontawesome-free": "^5.4.1",
        "express": "^4.16.4",
        "index.js": "0.0.3",
        "moment": "^2.22.2",
        "packages": "0.0.8"
      },
      "engines": {
        "node": "8.12.0",
        "engines": {
          "npm": "6.4.1"
        }
      }
    }

  3. Procfile

    web: node index.js

Pls clearly point out the issues I might be having and errors in the files I'm working with as I'm new to all these

2 Answers

Heroku isn't a FaaS service, it's a PaaS. I see that you've exported a function, but it ends there. Ensure that you're running a long-running application i.e. daemon. As stated clearly in logs, your application doesn't wait for anything and directly exits with return code 0 which indicates that the program has finished execution successfully. I'm not a Node.js developer but having a look at the Hello World example by ExpressJS will most likely help you understand why things didn't work this time.

https://expressjs.com/en/starter/hello-world.html


app.listen(port, () => console.log(`Example app listening on port ${port}!`))

This line does the work in the code snippet by ExpressJS. It starts listening to incoming connections, so it blocks forever until killed and accepts connections.

Case 1: Procfile
in Procfile there should be no spaces between web: and node index.js.

  • incorrect:
    web: node index.js
  • correct:
    web:node index.js

Case 2: devDependency
There should not be any dev dependencies on the server, as heroku didn't install devDependencies by default. So either change devDependencies to dependencies in your package.json, or run:

heroku config:set NPM_CONFIG_PRODUCTION=false
Related