gpt4 book ai didi

node.js - 如何让NodeJS应用程序在PAAS上自动运行?

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:22 25 4
gpt4 key购买 nike

我用 meteor 编写了一个简单的应用程序。我使用demeteorizer来消除它对 meteor 的依赖。现在我已将我的 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 文件,我将其与 bundle 的其余部分一起上传到 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 文档,在启动 Node 之前我必须设置一些环境变量。从命令行使用以下命令,我可以成功运行我的应用程序。

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

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

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

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

更多信息

以下是 Node 如何从实例运行:

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 start 在服务器启动时发生?

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

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

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

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

欢迎提出建议。

关于node.js - 如何让NodeJS应用程序在PAAS上自动运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com