Nathan
Nathan

Reputation: 2387

Trying to deploy app on openshift. "program node app.js exited with code 8". Can't figure out what's going on

I'm pretty new at this. This is my first time trying to deploy an app in a production environment. It works totally fine locally (and of course with local environment variables instead of Openshift's environment variables). But on openshift I just can't get it to run. It always responds with "503 Service Temporarily unavailable". I've been tinkering with it for hours, and looking everywhere for a solution, but nothing is working.

Here's what I have in my index.js file (it's just a simple mongodb utilizing to-do list. And like I said, it works fine locally, but yes, it's very messy. Like I said. I'm new):

var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;
var methodOverride = require('method-override');
var session = require('express-session');

var app = express();
var router = express.Router();

var dbUrl = 'mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/todo_list';

var result;
var list;

var port = process.env.OPENSHIFT_NODEJS_PORT;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride(function(req, res){
  if (req.body && typeof req.body === 'object' && '_method' in req.body) {
    // look in urlencoded POST bodies and delete it
    var method = req.body._method
    delete req.body._method
    return method
  }
}));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));

function updateList(res){
    MongoClient.connect(dbUrl, function(err, db){
        if(err){console.log(err);}
        var list = db.collection('list');
        list.find({}).toArray(function(err, docs){
            res.render('index', { title: 'To do:', list: docs });
        });
    });
};

//routes

app.get('/', function(req, res){
    updateList(res);
});

app.post('/', function(req, res){
    var data = req.body.words;
    MongoClient.connect(dbUrl, function(err, db){
        var list = db.collection('list');
        list.insert({ task: data }, updateList(res));
    });
});

app.delete('/:id', function(req, res){
    var removal = req.params.id;
    MongoClient.connect(dbUrl, function(err, db){
        var list = db.collection('list');
        list.remove({ _id: ObjectId(removal)}, updateList(res));
    });
    res.redirect('/');
});

var server = app.listen(port, function(){
    console.log('listening');
});

module.exports = server;

And then here's what's logged out to me:

Error: listen EACCES
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1020:19)
    at listen (net.js:1061:10)
    at Server.listen (net.js:1135:5)
    at EventEmitter.listen (/var/lib/openshift/<key>/app-root/runtime/repo/node_modules/express/lib/application.js:617:24)
    at Object.<anonymous> (/var/lib/openshift/<key>/app-root/runtime/repo/index.js:66:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
DEBUG: Program node index.js exited with code 8
DEBUG: Starting child process with 'node index.js'

I'm at a loss. Please help.

Upvotes: 0

Views: 836

Answers (1)

snozza
snozza

Reputation: 2253

You also need to bind to the OPENSHIFT_NODEJS_IP environment variable. This can be done in the second argument of app.listen, like as follows:

var ipAddress = process.env.OPENSHIFT_NODEJS_IP
var server = app.listen(port, ipAddress, function(){
  console.log('listening');
});

If you don't do this, the default IP address will be 0.0.0.0 which won't work on Openshift

Upvotes: 1

Related Questions