Aitzol Berasategi
Aitzol Berasategi

Reputation: 71

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. 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.

Here is my upstar file 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:

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.

Upvotes: 1

Views: 2175

Answers (2)

siavolt
siavolt

Reputation: 7077

You need use __dirname in your api.js. For example:

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

Upvotes: 0

Robert Rossmann
Robert Rossmann

Reputation: 12129

All fs module's methods operate relative to the current working directory (cwd) when relative paths are given, see the docs.

Given the above, you need to tell forever to start your application with correct working directory - this can be done via the --workingDir flag. See the docs again.

Upvotes: 2

Related Questions