如何使PAAS autorun上的NodeJS应用程序?

我在meteor中写了一个简单的应用程序。 我用demeteorizer去除它对meteor的依赖。 现在,我已经将我的卸载包上传到Gandi NodeJS 简单托pipe实例 。 我可以让它从控制台运行,但是我不能让它在我重新启动实例时自动运行。

我将默认的server.js移出实例启动时运行的方式。 这是它包含的内容:

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"); 

在我的本地机器上运行demeteorizer,它创build了一个project.json文件,我将其余的包上传到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 

根据demeteorizer文档,有几个envvariables我必须设置才能启动节点。 从命令行使用以下命令,我可以成功运行我的应用程序。

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

(这些价值有点违反直觉,与许多定义教程的内容相矛盾,但是直接来自定义者的文档和作品。)

鉴于有限的访问,我必须简单的托pipe启动脚本,我不知道如何启动我的应用程序,当节点启动或如何设置环境variables之前运行。

你能帮我弄清楚如何让我的应用程序在PAAS实例启动时运行?

更多信息

以下是从该实例运行节点的方式:

 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 

最后,我通过摆弄弄明白了这一点。 虽然我认为可能有一个更清洁的解决scheme。

我将我的envvariables添加到project.json脚本指令中的起始行

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

我证实了这一点

 npm start 

现在,当服务器启动时如何让npm启动?

我将永久监视器安装为JS包,但不是CLI,因为我无法从PAAS实例的控制台访问系统。

然后我创build了一个简单的server.js,在实例启动时自动运行:

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

欢迎提出build议。

现在可以使用PORT envvariableshttp://wiki.gandi.net/en/simple/instance/nodejs?#general_functioning