Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Restarting your node.js app on code change using pm2

    Will ButtonWill Button

    In this video, learn how to automatically restart your node.js app using pm2 when the code for your app changes. You will also learn how to exclude files and directories from restarting your app (such as static assets, css, images, etc.).

    pm2PM2
    nodeNode.js
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:01 One of the other cool features of PM2 is the ability to watch your application directories and restart your application if it detects a change to the file, much like if you were doing a push or had a hook on your version control system that pushed the code out when there was a change to a branch.

    00:22 If we want to set that up, we go back into our config file, and we'll just add a new line here called Watch, and we'll set that equal to true.

    00:35 Watch also comes with a counterpart, called Ignore Watch, and as the name implies, it will ignore whatever is defined here. It accepts an array, and inside the array you can put in file names, folder names, paths, and ReGex expressions, and everything that matches inside of the array will be ignored by the restart process.

    00:57 Let's save, and exit out of that, and you'll see that the reason I chose the word to put files in there, is because inside my app one folder I have a subdirectory called files.

    01:09 Let's go ahead and pull PM2 up now, with a PM2 start, and then our configuration object. We can see both apps launch.

    01:19 Now, this time in our watching column, under app one, you can see that Watching is enabled, and since we did not define it for app two, it's set to disabled. Let's test that and see how it works.

    01:33 We go into our app one, serve it as JS file. We can just make a little change here that says, "Hello from app one after a reload." If we save and quit, we get a PM2 list, you can see that the PID for app one changed from 7861 to 7876, while the PID for app two has remained the same at 7862, because watching is not set up for that.

    02:06 If we switch over to our browser here and load the page, we get "hello from app one," after a reload, and then we switch back over to app two, and we still just get "hello from app two."

    02:21 We also put in our Ignore Watch there, so we've got to test that and see how that works. If we edit a file inside of the files folder, we can just put "this is not valid HTML," save that and quit.

    02:42 Now app one was running on a PID of 7876, so if we type PM2 list, we can see that even though we changed the file, it's still running on PID 7876, because that was in an ignored folder, and if we load our page, we still get "Hello from app one" after a reload, so everything's working just like we want.

    Discuss

    Discuss