Lazyexpert
Lazyexpert

Reputation: 3154

Deploying nodejs app to Openshift (503 error)

Having an issue deploying first node js app to openshift.

I am a little confused with ports.

I've set this when server is created:

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080,
    server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'

http.createServer(function (request, response) {
  return router(request, response);
}).listen(port, server_ip_address, function() {
  console.log( "Listening on " + server_ip_address + ", server_port " + port )
})

I've logged the port. I receive 8080. But, if I want to listen to websockets I have to set manually to 8080 also(on receive) and I believe, this leads me to an error, listening same port twice.

Reading logs also prooves, that app falls on ws server create method. Where port, I bet, is the reason. But, how to fix?

Is there any option, that I can't still see?

Update: Changed websocket port to 8000. The error seems to be the same:

Error: listen EACCES
at errnoException (net.js:905:11)
    at Server._listen2 (net.js:1024:19)
    at listen (net.js:1065:10)
    at Server.listen (net.js:1139:5)
    ...

But now works locally.

Update2: I've managed to switch to one port usage. My mistake, now I am passing a http server as argument to websocket server create method. Still works locally. Code at the moment:

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080,
    server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'

var server = http.createServer(function (request, response) {
  return router(request, response);
})

server.listen(port, server_ip_address, function() {
  console.log( "Listening on " + server_ip_address + ", server_port " + port )
})

It falls on server listen line...

Update3: I've added eventlistner for server's event "listening" and locally it fires. But according to logs on openshift it does not. The error is still the same:

Error: listen EACCES
    at errnoException (net.js:905:11)
    at Server._listen2 (net.js:1024:19)
    at listen (net.js:1065:10)
    at Server.listen (net.js:1139:5)
    at Server.server.listen (/var/lib/openshift/56fc344f2d5271249e0000fe/app-root/runtime/repo/node_modules/sesh/lib/core.js:56:12)
    at Object.<anonymous> (/var/lib/openshift/56fc344f2d5271249e0000fe/app-root/runtime/repo/server.js:18:15)
    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 server.js exited with code 8
DEBUG: Starting child process with 'node server.js'

I'm out of ideas. Appreciate any help. Thanks, best regards.

Upvotes: 1

Views: 420

Answers (1)

Lazyexpert
Lazyexpert

Reputation: 3154

Well, I couldnt find the exact problem, but was able to localize it.

So the starting app structure was:

  • no express
  • http.server (listen)
  • ws.server (listen)
  • static files server (listen)
  • sesh (sessions)

This all thing is working perfectly locally.

For openshift deploy, I had to do these changes, to start working normally:

  • installed express
  • using only one listen
  • somewhy sesh module didn't work for me, I've switched to 'express-session'

The final code looks like this (hope someone will find this useful):

'use strict';

var http = require('http'),
    express = require('express'),
    app = express()


var router = require('./modules/router')
var session = require('express-session')

app.use(session({
  secret: 'two keyboard cats',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}));

app.use(router)

app.use(express.static('public'))

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080,
    server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'

var server = http.createServer(app)
server.listen(port, server_ip_address, function() {
  console.log("listening to the server")
})

And router module is this like:

'use strict';
var router = require('express').Router();

router.get('/', function(req, res) {
  console.log(req.session.id)
  res.writeHead(302, { 'Location': '/index.html' })
  res.end()
})


module.exports = router

And advice for "noobs" like me: pay attention to the order of middlewares. Sessions should go before router.

Upvotes: 1

Related Questions