ExpressJS / Heroku应用程序错误

时间:2017-08-28 06:48:08

标签: node.js express heroku

我正在尝试在Heroku上部署测试快速应用程序。

我的package.json看起来像这样 -

{
  "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"
  }
}

我的Procfile看起来像这样 -

web: npm start

我的测试应用就是这样 -

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;

在本地运行我的应用程序时,它会启动并完美运行。运行heroku本地Web时,它也可以按预期工作。

然而,当我部署到heroku时,我收到了应用程序错误页面。但是在流式传输我的日志时,没有任何迹象表明它已经崩溃了。

我刚刚再次部署,这是当前的日志文件

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

我不知道如何继续这一点。

1 个答案:

答案 0 :(得分:1)

您不应将http服务器的端口设置为3000,您应使用process.env的端口,heroku将在PORT中设置process.env

所以你应该使用app.listen(process.env.PORT, () => {})

并且有一个关于heroku应用程序的错误代码,如果你可以提供,那就更好了。