Heroku上的NodeJs / ExpressJs应用程序错误

时间:2018-06-23 10:13:24

标签: node.js heroku

我正在尝试部署我的应用程序,但是即使应用程序API在本地运行得很好,也遇到了应用程序错误。

我尝试了很多通过搜索提供的解决方案,但我的可能与众不同。

这是我的应用和下面的依赖项

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
var fs = require('fs');
var data = fs.readFileSync('data.json');
var rideArray = JSON.parse(data);
var serveStatic = require('serve-static');
var port = process.env.port || 3000;


app.use('/api/v1', serveStatic(path.join(__dirname, 'views')))

app.get('/api/v1/rides', getRideOffers=(req, res)=>{
    res.send(rideArray)
});

app.get('/api/v1/rides/:id', getRideOffers=(req, res)=>{
    let id = req.params.id
    res.send("Here are ride offers for " + id)
});

app.listen(port);

console.log("Way to go server!")

{
  "name": "rideapp",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "darot",
  "license": "ISC",
  "dependencies": {
    "body-parser": "*",
    "ejs": "^2.6.1",
    "eslint": "^4.19.1",
    "express": "^4.16.3",
    "pug": "^2.0.3",
    "serve-static": "^1.13.2"
  },
  "devDependencies": {
    "dotenv": "^6.0.0"
  },
  "keywords": [],
  "description": ""
}

和日志。也许我尤其可以从日志中获得帮助,但无法修复。

2018-06-23T09:46:25.259919+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

2018-06-23T09:46:25.260021+00:00 heroku[web.1]: Stopping process with SIGKILL

2018-06-23T09:46:25.366760+00:00 heroku[web.1]: Process exited with status 137

2018-06-23T09:46:25.493839+00:00 heroku[web.1]: State changed from starting to crashed

2018-06-23T09:46:26.261078+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=ridemypath.herokuapp.com request_id=1c5b608d-d6cd-424f-a384-ed6077bdaf8b fwd="197.210.44.202" dyno= connect= service= status=503 bytes= protocol=https

1 个答案:

答案 0 :(得分:0)

我能够通过下面的代码使用port.env.PORT来解决此问题

const port = process.env.PORT || 8080;
app.listen(port, () => {
    console.log("Way to go server at port " + port);
});

谢谢