Last Updated: October 06, 2018
·
8.841K
· Kyle Ross

Running Node.js Application on OpenShift with a filename other than server.js

I spent about an hour tonight scratching my head to figure out what I did wrong while setting up my new Node.js application in OpenShift. The problem I was facing was with the file name of the main application/server. By default, they give you a sample application that resides in server.js although I like my main application file to be named app.js. Not a big deal in most cases, except at OpenShift.

Their action hooks that build and deploy your application after git push is set to run a command similar to node server.js which would need to be changed using a custom action hook for your application to override it and use a different file name. Most Google searches will bring back that the main parameter in your package.json would need to be changed to the correct file name, although after testing several times, I could not get it to work. Basically, I would deploy the application and after waiting about 5 minutes, my site would be still sitting at a 503 "Application Unavailable" error screen. Checking through SSH, node isn't even running!

There is a hidden gem within OpenShift that helps remedy this issue on deploy. In your pacakge.json file, you can add a snippet to set the command to be ran on deploy. Once I added this, deployed the application, I had a working website!

"scripts": {
    "start": "node app.js --websocket-port=$OPENSHIFT_NODEJS_PORT --log-level=1"
}

You can change app.js to your main application file if you decide to name it something else.

I spent quite a bit of time trying to remedy this solution, so I hope it will help some others out there that run into the same issue on OpenShift and do not want to follow their methodology. If someone has figured out an even better way of accomplishing this, please post :)

3 Responses
Add your response

Nice, but I still getting 503. Which port do you use to start node.js in the app?

over 1 year ago ·

I don't understand why, but "git push" is not synchronizing the local version of my app with the one on the server.

over 1 year ago ·