我正在使用node-windows将我的应用程序设置为作为Windows Service运行。我正在使用node-config管理配置设置。当然,当我使用node app.js命令手动运行我的应用程序时,一切工作正常。当我将其作为服务安装并启动时,配置设置为空。我在./config文件夹中有production.json文件,我可以在安装脚本中将NODE_ENV设置为production。我可以确认变量设置正确,但仍然没有结果。即使我在服务的env值中显式地设置了它,log.info('CONFIG_DIR: ' + config.util.getEnv('CONFIG_DIR'));也会生成undefined。寻找任何洞察力。
安装脚本:
var Service = require('node-windows').Service;
var path = require('path');
// Create a new service object
var svc = new Service({
name:'Excel Data Import',
description: 'Excel Data Import Service.',
script: path.join(__dirname, "app.js"), // path application file
env:[
{name:"NODE_ENV", value:"production"},
{name:"CONFIG_DIR", value: "./config"},
{name:"$NODE_CONFIG_DIR", value: "./config"}
]
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();应用脚本:
var config = require('config');
var path = require('path');
var EventLogger = require('node-windows').EventLogger;
var log = new EventLogger('Excel Data Import');
init();
function init() {
log.info("init");
if(config.has("File.fileFolder")){
var pathConfig = config.get("File.fileFolder");
log.info(pathConfig);
var DirectoryWatcher = require('directory-watcher');
DirectoryWatcher.create(pathConfig, function (err, watcher) {
//...
});
}else{
log.info("config doesn't have File.fileFolder");
}
}发布于 2015-07-07 01:05:53
我知道这个回复很晚,但我也遇到了同样的问题,以下是我是如何解决它的:
var svc = new Service({
name:'ProcessName',
description: 'Process Description',
script: require('path').join(__dirname,'bin\\www'),
env:[
{name: "NODE_ENV", value: "development"},
{name: "PORT", value: PORT},
{name: "NODE_CONFIG_DIR", value: "c:\\route-to-your-proyect\\config"}
]
});使用windows时,不需要为环境变量加上前缀$。
此外,当您的运行脚本与您的配置目录不在同一目录下时,您必须提供您的配置目录的完整路径。
当您在node-windows中遇到错误时,深入挖掘错误日志也很有帮助。它位于rundirectory/daemon/processname.err.log
我希望这能对某些人有所帮助。
https://stackoverflow.com/questions/25172452
复制相似问题