How to make NodeJS app on PAAS autorun?

534 views Asked by At

I wrote a simple app in meteor. I used demeteorizer to remove its dependence on meteor. Now I've uploaded my demeteorized bundle to a Gandi NodeJS simple hosting instance. I can get it to run from the console, but I can't get it to automatically run when I reboot the instance.

I moved the default server.js out of the way which runs when the instance boots up. Here's what it contained:

var http = require("http");

http.createServer(function(req, res) {
    res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
    res.end('<!DOCTYPE html><html><meta charset="utf-8"><title>It works' +
            "</title><b>It works!</b><br /><br />This is the server's " +
            "default server.js.");
}).listen(8080);
console.log("Server ready to accept requests on port 8080");

Running demeteorizer on my local machine, it created a project.json file which I uploaded with the rest of the bundle to vhosts/default dir:

hosting-user@Secret-History-Node-Test:~/web/vhosts/default$ more package.json
{
  "name": "secrethistory",
  "description": "secrethistory - automatically converted by Demeteorizer. https
://github.com/onmodulus/demeteorizer",
  "version": "0.0.1",
  "main": "main.js",
  "scripts": {
    "start": "node main.js"
  },
  "engines": {
    "node": "0.10.36"
  },
  "dependencies": {
    "websocket-driver": ">=0.4.0",
    "faye-websocket": "^0.7.3 || ^0.8.0",
    "node-uuid": "^1.4.1",
    "sockjs": "https://registry.npmjs.org/sockjs/-/sockjs-0.3.11.tgz",
    "es5-ext": "~0.9.2",
    "event-emitter": "~0.2.2",
    "next-tick": "0.1.x",
    "memoizee": "~0.2.5",
    "cli-color": "https://registry.npmjs.org/cli-color/-/cli-color-0.2.3.tgz",
    "css-parse": "https://github.com/reworkcss/css-parse/tarball/aa7e23285375ca6

According to the demeteorizer docs, there are a few env variables I have to set before I can start node. Using the following from the command line, I can run my app successfully.

export MONGO_URL=mongodb://localhost:27017/secrethistory 
export PORT=8080 
export ROOT_URL=http://localhost:8080
node main

(Those values are a little counterintuitive and contradict what many demeteorizer tutorials state, but it comes straight from the demeteorizer docs and works.)

Given the limited access I have to the simple hosting startup scripts, I don't know how to start my app when node starts or how to set the environment variables before it runs.

Can you help me figure out how to get my app to run when the PAAS instance is booted?

More Info

Here's how node is run from the instance:

hosting-user@Secret-History-Node-Test:~/web/vhosts/default$ ps -ef | grep node
5000        73     1  0 06:06 ?        00:00:00 python /srv/admin/scripts/watchd --logfile /srv/data/var/log/www/nodejs-watchd.log --pidfile /srv/run/nodejs/nodejs-watchd.pid --app-logfile /srv/data/var/log/www/nodejs.log --app-dir /srv/data/web/vhosts/default /srv/admin/scripts/nodejs/node-bootstrap.sh
2

There are 2 answers

1
Wes Modes On BEST ANSWER

In the end I figured this out by fiddling. Though I think there may be a cleaner solution.

I added my env vars to the start line in the scripts directive in project.json

"start": "MONGO_URL=mongodb://localhost:27017/secrethistory PORT=8080 ROOT_URL=http://localhost:8080 node main.js"

I confirmed that this worked with

npm start

Now, how to make npm start happen when the server boots?

I installed forever-monitor as a JS package, but not the CLI since I don't have access to the system from the console of the PAAS instance.

Then I created a simple server.js which is run automatically when the instance boots:

var forever = require('forever-monitor');

var child = forever.start(['npm start'], {
    'max': 3,
    'silent' : true
});

Suggestions are welcome.

0
William Dauchy On

the PORT env variable is now available by default http://wiki.gandi.net/en/simple/instance/nodejs?#general_functioning