njcr7w
Last Updated: May 03, 2016
·
17.34K
· mikeymike
5ed4987940acbeb640b2beaa1344b433

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

13438

Awesome tips! Thanks :)

over 1 year ago ·
14207
Da0c8b45553a88ddfc634197c8dd32f7

Dude, you rock!

over 1 year ago ·
14402
O5o9jbcy normal

please use forever logs <script|index> -f

over 1 year ago ·
14432
95ad0e8c265950b42db9569ad40cce0f

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

Thanks.

over 1 year ago ·
14929
199046437b76e6ca73e00b4cc182a1c5

nice :)

over 1 year ago ·
14973
Da22d651ae136602e4a24d029972d054

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 ·
15252
Valentin

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

over 1 year ago ·
15877

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

over 1 year ago ·
16430
34677a06aa6ee7c73844e25df1237c15

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 ·
17149
None

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 ·
18707
Bbfdaac771cd7f9894c18558fb29b485

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 ·
22129
Vqp7emcz normal

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

over 1 year ago ·
22281
0 yfrwqkh2ddcp47yqoqwqq 4slakcz2yqp6fqq mavikpbewn 3ibhtd0i8ryrig4xxjvwvbrbhx0

Hi

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

why?

over 1 year ago ·