pgGriff pgGriff - 2 months ago 20
Node.js Question

ExpressJS / Heroku Application Error

I am attempting to deploy a test express app on Heroku.

My package.json looks like this -

{
"name": "heroku-test-app",
"version": "1.0.0",
"description": "",
"main": "dist",
"scripts": {
"test": "cross-env NODE_ENV=test nodemon --exec mocha ./**/*.spec.js --compilers js:babel-core/register --require babel-polyfill",
"dev": "nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"",
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "cross-env node dist",
"prestart": "npm run -s build",
"clean": "rimraf dist/"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^7.2.3",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"body-parser": "^1.17.2",
"cross-env": "^5.0.5",
"dotenv": "^4.0.0",
"express": "^4.15.4",
"morgan": "^1.8.2"
},
"devDependencies": {
"chai": "^4.1.1",
"mocha": "^3.5.0",
"nodemon": "^1.11.0",
"rimraf": "^2.6.1",
"supertest": "^3.0.0"
}
}


My Procfile looks like this -

web: npm start


and my test app is as so -

import express from 'express';
import morgan from 'morgan';
import bodyParser from 'body-parser';

const app = express();

const postBodyLimit = '100kb';
const port = 3000;

app.use(
morgan('combined', {
skip: (req, res) => res.statusCode < 400
})
);

app.use(bodyParser.json({ limit: postBodyLimit }));
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', (req, res) => res.status(200).json({ message: 'No unicorns here.' }));

// catch 404's and pass to our global error handler
app.all('*', (req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});

app.use((err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message,
error: app.get('env') === 'development' ? err : null
});
});

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

export default app;


When running my app locally, it starts and works perfectly. When running heroku local web, it also works as expected.

When I deploy to heroku however, I get the application error page. However on streaming my logs, there is nothing to suggest it has crashed.

I have just deployed again now, this is the current log file

2017-08-28T06:38:40.000000+00:00 app[api]: Build succeeded
2017-08-28T06:39:03.959631+00:00 app[web.1]:
2017-08-28T06:39:03.959649+00:00 app[web.1]: > heroic-test-app@1.0.0 prestart /app
2017-08-28T06:39:03.959650+00:00 app[web.1]: > npm run -s build
2017-08-28T06:39:03.959651+00:00 app[web.1]:
2017-08-28T06:39:05.856118+00:00 app[web.1]: src/index.js -> dist/index.js
2017-08-28T06:39:05.898228+00:00 app[web.1]:
2017-08-28T06:39:05.898232+00:00 app[web.1]: > heroic-test-app@1.0.0 start /app
2017-08-28T06:39:05.898234+00:00 app[web.1]: > cross-env node dist
2017-08-28T06:39:05.898234+00:00 app[web.1]:
2017-08-28T06:39:06.654100+00:00 app[web.1]: listening on port 3000
2017-08-28T06:44:40.078283+00:00 app[web.1]:
2017-08-28T06:44:40.078319+00:00 app[web.1]: > heroic-test-app@1.0.0 prestart /app
2017-08-28T06:44:40.078320+00:00 app[web.1]: > npm run -s build
2017-08-28T06:44:40.078321+00:00 app[web.1]:
2017-08-28T06:44:42.379605+00:00 app[web.1]: src/index.js -> dist/index.js
2017-08-28T06:44:42.434704+00:00 app[web.1]:
2017-08-28T06:44:42.434707+00:00 app[web.1]: > heroic-test-app@1.0.0 start /app
2017-08-28T06:44:42.434708+00:00 app[web.1]: > cross-env node dist
2017-08-28T06:44:42.434709+00:00 app[web.1]:
2017-08-28T06:44:43.156940+00:00 app[web.1]: listening on port 3000
2017-08-28T06:45:39.332668+00:00 app[web.1]: Error waiting for process to terminate: No child processes


I am at a loss how to proceed at this point.

Answer Source

you shouldn't set the port of http server as 3000, you should use the port of process.env, heroku will set an PORT in process.env.

So you should use app.listen(process.env.PORT, () => {}).

and there is an err code about the heroku application, if you can provide that, it would be better.