Auto reloading a Sails.js app on code changes?

后端 未结 8 735
花落未央
花落未央 2020-11-30 18:38

Currently is seems that for any code change in a sails.js app you have to manually stop the sails server and run sails lift again before you can see the changes

相关标签:
8条回答
  • 2020-11-30 18:45

    You can use sails-hook-autoreload

    Just lift your app as normal, and when you add / change / remove a model or controller file, all controllers and models will be reloaded without having to lower / relift the app.

    0 讨论(0)
  • 2020-11-30 18:50

    Better you use

    npm install -g nodemon
    

    i am using this, and it will helps to improve my developing speed. no need to edit any files for this one!.

    after installation

    nodemon app.js
    
    0 讨论(0)
  • 2020-11-30 18:51

    You have to use a watcher like forever, nodemon, or something else...

    Example

    1. Install forever by running:

      sudo npm install -g forever

    2. Run it:

      forever -w start app.js


    To avoid infinite restart because Sails writes into .tmp folder, you can create a .foreverignore file into your project directory and put this content inside:

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

    See the issue on GitHub: Forever restarting because of /.tmp.

    0 讨论(0)
  • 2020-11-30 18:51

    For example with nodemon to watch api and config directories

    .nodemonignore contents

    views/*
    .tmp/*
    .git/*
    

    Run the command after creating .nodemonignore

    $> nodemon -w api -w config
    

    Example for supervisor to ignore 3 directories

    $> supervisor -i .tmp,.git,views app.js
    
    0 讨论(0)
  • 2020-11-30 18:56

    I had the same problem and I have solved it using grunt-watch and grunt-forever with sails@beta tasks. The result is 4 grunt commands:

    UPDATE: tasks are available in the current sails version (it's no longer beta :>)

    • start Starts the server
    • stop Stops the server
    • restart Restarts the server
    • startWatch Starts the server and waits for changes to restart it (using grunt-watch). This is probably your solution, but the other commands are also useful.

    Here's the code - I'm using sails@beta, which includes a tasks directory, I don't know if this is included in previous versions:

    • First of all you have to install forever in your sails directory:

      npm install grunt-forever --save-dev
      
    • tasks/config/forever.js Configure forever task.

      module.exports = function(grunt) {
        grunt.config.set('forever', {
          server: {
             options: {
                index: 'app.js',
                logDir: 'logs'
             }
          }
        });
      
        grunt.loadNpmTasks('grunt-forever');
      };
      
    • tasks/config/watch.js (edit) Edit watch task in order to add a new rule

      // api and assets default rules
      ,
      server: {
          // Server files to watch:
          files: [
              'api/**/*',
              'config/**/*'
          ],
      
          // Restart server
          tasks: ['forever:server:restart']
      }
      
    • tasks/register/watchForever.js Register your custom tasks (this file can be renamed to whatever you want)

      module.exports = function(grunt) {
      // Starts server
        grunt.registerTask('start', [
          'compileAssets',
          'linkAssetsBuild',
          'clean:build',
          'copy:build',
          'forever:server:start'
        ]);
      
        // Restarts the server (if necessary) and waits for changes
        grunt.registerTask('startWatch', [
          'restart',
          'watch:server'
        ]);
      
        // Restarts server
        grunt.registerTask('restart', [
          'forever:server:restart'
        ]);
      
        // Stops server
        grunt.registerTask('stop', [
          'forever:server:stop'
       ]);
      };
      

    With this you should be able to use

        grunt startWatch
    

    and make your server wait for changes to be restarted :>

    Hope this helped!

    0 讨论(0)
  • 2020-11-30 19:00

    For anyone coming to this question now, it seems that this is no longer necessary - an application launched with sails lift will have a grunt watch task running, and code changes will be visible without a restart.

    I didn't realise this was happening at first because there's nothing to indicate what's happening in the console, but it does seem to work without a restart (I'm using Sails 0.11)

    0 讨论(0)
提交回复
热议问题