简体   繁体   中英

forever node.js - watch directory for file changes

I want my node application index.js to be restarted if there is any file change detected in its directory or below. Additionally, I want the process to be in the foreground, outputting logs to the terminal. What is the command?

My tries:

forever stopall

forever -w /home/patrick/workspace/frontend-api/index.js

Result:

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Could not read .foreverignore file.
error: ENOENT, open '/.foreverignore'
error: restarting script because unlinkDir changed

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: watch EACCES
    at errnoException (fs.js:1024:11)
    at FSWatcher.start (fs.js:1056:11)
    at Object.fs.watch (fs.js:1081:11)

From what I understand (the documentation on this is non-existent from what I can see). the -w or --watch function makes forever check for a .foreverignore file, and if this is missing the program essentially fails and gets stuck in a loop never starting the module.

in your app directory create a file called .foreverignore and list everything that you do not want forever to watch. This basically tells forever to ignore changes to these files and not to restart if anything happens to them, which is great for log files or things that don't actually require your module to restart to get the benefits from.

Here's an example taken from my implementation:

File:

/apps/myapp/.foreverignore

Contents

node_modules/*
logs/*
conf/*
htdocs/*
*.log
*.gif
*.jpg
*.html

Once created make sure you restart forever for the file to be picked up.

seems like u need nodemon

npm install -g nodemon

however in my windows server i used https://github.com/tjanczuk/iisnode good for scalability on multi core servers

As explained by @stuffyjoelab forever -w checks for a .foreverignore file, and will get stuck if it's not there.

Here's my simple approach to adding forever to a node server project.

  1. create .foreverignore

     # we only care about changes to javascript files # in the src folder !src/*.js 
  2. npm i -D forever to add forever as a dev-dependency

  3. add this to the scripts in your package.json file

     "dev": "forever -w --minUptime=1000 --spinSleepTime=1000 index.js", 
  4. npm run dev to start it up with forever.

  5. Change a file and voila — your server reboots

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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