I am having trouble starting my node app in Bluemix

北城以北 提交于 2019-12-05 08:34:44

I found out it was an issue with Cloud Foundry starting my app.

In digging into my code I was trying to bind to port 8080 locally. That is fine, but however in Cloud Foundry you need to bind to a specified port. To do this you need to read an environment variable called VCAP_APP_PORT. I have pasted a code snippet below on how I fixed it.

var express = require("express"),
    app = express();


var port = process.env.VCAP_APP_PORT || 8080;

app.get("/", function (request, response) {
    response.render("index");
});

app.listen(port);

Another suggestion: Add this prior to the call to app.listen(...)

process.on('uncaughtException', function (err) {
    console.log(err);
}); 

Your console logs will then include a helpful stack trace if the startup code fails for an unforeseen reason instead of receiving the default "app crashed" message.

priya2503

You can get this text off of the .stack property from any Error. For instance:

try {
  throw new Error();
} catch (e) {
  console.log(e.stack);
}

or just new up an error for the purposes of getting the stack trace with console.log(new Error().stack). This code snippet will get the stack trace at any point, not just in a catch block.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!