如何让 NodeJS 应用在 PAAS 上自动运行?

How to make NodeJS app on PAAS autorun?

我用流星写了一个简单的应用程序。我使用 demeteorizer 来消除它对流星的依赖。现在我已经将我的 demeteorized 包上传到 Gandi NodeJS simple hosting instance。我可以从控制台将它设置为 运行,但是当我重新启动实例时我无法将它自动​​设置为 运行。

我将默认值 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 在我的本地机器上,它创建了一个 project.json 文件,我将其与捆绑包的其余部分一起上传到 vhosts/default 目录:

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 文档,在启动节点之前我必须设置一些环境变量。从命令行使用以下命令,我可以 运行 我的应用成功。

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

(这些值有点违反直觉,与许多除流器教程中的说法相矛盾,但它直接来自 demeteorizer docs 并且有效。)

鉴于我对简单托管启动脚本的访问权限有限,我不知道如何在节点启动时启动我的应用程序或如何在它之前设置环境变量运行s。

你能帮我弄清楚如何在启动 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

最后我通过摆弄弄明白了。尽管我认为可能有更清洁的解决方案。

我将环境变量添加到 project.json

中脚本指令的起始行
"start": "MONGO_URL=mongodb://localhost:27017/secrethistory PORT=8080 ROOT_URL=http://localhost:8080 node main.js"

我确认这适用于

npm start

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

我安装了 forever-monitor 作为 JS 包,但没有安装 CLI,因为我无法从 PAAS 实例的控制台访问系统。

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

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

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

欢迎提出建议。

PORT 环境变量现在默认可用 http://wiki.gandi.net/en/simple/instance/nodejs?#general_functioning