Last Updated: September 09, 2019
·
28.41K
· mikeymike

Sails.js, sick of restarting your server ?

This is probably only applicable to beginners as I can only assume experienced users have already had this issue and quickly did what I have and found the solution. But for those of you who haven't then make sure to do this when developing your next Sails application.

Note: I'm assuming you already have Sails installed

Install forever. (Other packages are available for this, but this is what I use)

[sudo] npm install forever -g

Create your new Sails.js application wherever you want

sails new awesomeApplication
cd awesomeApplication

Now you want to add a file called .foreverignore

nano .foreverignore

Add the following lines to the file

**/.tmp/**
**/views/**
**/assets/**

If you have Sails installed globally then you will have to run the following command, which will add Sails to your application.

npm install sails

Now when you lift your application instead of using

sails lift

you use...

forever -w start app.js # -w to watch for file changes!

Now you can enjoy coding your application and let Forever handle restarting the server when changes are made.

But wait now forever launches it in the background and I can't see my console.logs...

No problem!, in your terminal find your .forever location generally in your Home folder and run the following

See update for a better way than this...

# List running apps to get log file name
forever list

ls #list the files to find the log file
0P9R.log    9UBR.log    KqgI.log    config.json ehQC.log    pids        qvyg.log    sock

tail -f KqgI.log

To stop your application just run

forever stop app.js

Update

Thanks to zieglar for the tip

Instead of using tail command you can just use forever to stream it out with...

forever logs app.js -f

If you have multiple applications running which are all called app.js you will have to use the index so just grab that from the list (it will be in square brackets) by using

forever list

# Start the log using the index
forever logs 2 app.js -f

13 Responses
Add your response

Awesome tips! Thanks :)

over 1 year ago ·

Dude, you rock!

over 1 year ago ·

please use forever logs <script|index> -f

over 1 year ago ·

To use with coffeescript, add require('coffee-script/register'); to app.js.

Thanks.

over 1 year ago ·

nice :)

over 1 year ago ·

i'm adding a comment, with forever use --uid "appname" with your forever start command so forever -w --uid "appname" start app.js then you can do forever restart appname or forever logs appname -f
Happy Coding!

over 1 year ago ·

Nice! combining your tip with this ( http://labs.telasocial.com/nodejs-forever-daemon/ ) in an ansible provisioning role.

over 1 year ago ·

If you are using IntellJ as yout IDE, add **/.idea/** to .foreverignore.

over 1 year ago ·

This one works for me:

forever -w -f start app.js

It will start server, watch for files changes and stream logs to stdout. Cool?

over 1 year ago ·

I use node-inspector with the --save-live-edit option. It allows you to edit node.js code in chrome dev tools while it's running.

over 1 year ago ·

What the hell is that !!
Too complicated !!!
You juste have to :

1) npm install -g nodemone

2) run your app.js with nodemon

3) PROFIT !

and it work like a charm :D

over 1 year ago ·

Using "sails-hook-autoreload" for model / api/*/.js & other config related changes to automatically reflect.

over 1 year ago ·

Hi

I have a problem, I do all steps, but forever not watching my project's changes

why?

over 1 year ago ·