Deploying nodejs app with Open Shift PaaS

2019-03-20 18:57发布

问题:

I'm starting with Open Shift PaaS and I picked a node.js app as my first try.

So far I'm getting a 503 when I try to access my app site.

I've tried:

  • Set my github repo,
  • set the ssh key at github and made Open Shift have it,
  • created a very simple app using express (I did install express by npm)
  • did commit and pushed all changes,
  • I did install rhc and ran rhc app restart -a nodejs

But I can't get it to work.

The rhc tail -a nodejs outputs:

DEBUG: Error: Cannot find module '/var/lib/openshift/531be41fe0b8cd3d12000003/app-root/runtime/repo/server.js'
    at Function._resolveFilename (module.js:337:11)
    at Function._load (module.js:279:25)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)

DEBUG: Program node server.js exited with code 1

DEBUG: Starting child process with 'node server.js'

This is my app.js file:

var express = require('express')
var server = new express()
server.use(express.static(__dirname+"/public"))


server.get('/', function (request, response) {
    response.send(200)
})

server.listen(process.env.OPENSHIFT_NODEJS_PORT || 80)

EDIT

I've renamed app.js to server.js, did git add, commit and push, restarted app with rhc. but it's not working yet though.

回答1:

You can name your main script whatever you want, as long as the contents of your package.json file accurately describes your recommended server init process

I've written up a blog post that outlines the two main steps for getting your Node.js project to run on OpenShift:

  1. Update your package.json file, documenting your server init process (main, scripts.start).
  2. Adapt your code to rely on environment variables for runtime configuration details: server ip, port, domain name, db passwords, and other custom tokens

Make sure to document your preferred deployment process in your project's README file as well