简体   繁体   English

如何为通过永久启动的Node.js应用程序设置正确的路径

[英]How to set correct paths for nodejs application launched via forever

i launch my nodejs application using this upstart script via forever, and it works fine. 我永远使用这个upstart脚本启动我的nodejs应用程序,它工作正常。 The problem is that when the application create a folder programmatically, it creates on root directory; 问题是,当应用程序以编程方式创建文件夹时,它会在根目录上创建;

/public/ newfolder instead of /var/www/nodeapp/public/ newfolder . / public / newfolder而不是/ var / www / nodeapp / public / newfolder

Here is my upstar file myapp.conf: 这是我的upstar文件myapp.conf:

#!upstart

#
description "Example upstart script for a Node.js process"

start on startup
stop on shutdown


expect fork


env NODE_BIN_DIR="/usr/bin"
env NODE_PATH="/usr/lib/node_modules"
env APPLICATION_PATH="/var/www/nodeapp/app.js"
env PIDFILE="/var/run/myapp.pid"
env LOG="/var/log/myapp.log"
env MIN_UPTIME="5000"
env SPIN_SLEEP_TIME="2000"

script
    # Add the node executables to the path, which includes Forever if it is
    # installed globally, which it should be.
   PATH=$NODE_BIN_DIR:$PATH

    exec forever \
      --pidFile $PIDFILE \
      -a \
      -l $LOG \
      --minUptime $MIN_UPTIME \
      --spinSleepTime $SPIN_SLEEP_TIME \
      start $APPLICATION_PATH
end script

pre-stop script
    # Add the node executables to the path.
    PATH=$NODE_BIN_DIR:$PATH

    exec forever stop $APPLICATION_PATH
end script

and the api.js that create the folder: 和创建文件夹的api.js:

newFolder = "public/"+folder_ID; //relative path for new folder
var mkdirp = require('mkdirp');

mkdirp(newFolder, function(err) { 
    if (err){
        console.log('error');
    }else{
        console.log('new folder');
    }

});

I hope you can help me, thanks. 我希望你能帮助我,谢谢。

All fs module's methods operate relative to the current working directory (cwd) when relative paths are given, see the docs . 当给出相对路径时,所有fs模块的方法都相对于当前工作目录(cwd)运行,请参阅文档

Given the above, you need to tell forever to start your application with correct working directory - this can be done via the --workingDir flag. 鉴于上述情况,您需要永远告诉您使用正确的工作目录启动应用程序 - 这可以通过--workingDir标志来完成。 See the docs again. 再次查看文档

You need use __dirname in your api.js. 你需要在你的api.js中使用__dirname。 For example: 例如:

newFolder = __dirname + "/public/"+folder_ID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM