使用forever和npm脚本运行节点

时间:2018-03-04 10:45:38

标签: node.js npm environment forever

系统:ubuntu 16

我在服务器上有一个工作节点应用程序。在package.json中有脚本,其中包含要执行的选项:

forever start ./bin/www

需要什么:

  • 使用可在服务器重启时启动应用程序的服务 保持它一直运行(我现在永远使用)。
  • 从根目录
  • 中的package.json源启动应用程序作为npm脚本

现在我可以通过转到app目录并输入以下命令永远开始:

forever start /home/myapp/bin/www

但是无法控制环境。

此外,当我从根目录尝试此操作时 - 它失败了:

var shuffleme = (function($) {
  'use strict';
  var $grid = $('#grid'), //locate what we want to sort 
    $filterOptions = $('.portfolio-sorting li'), //locate the filter categories
    $sizer = $grid.find('.shuffle_sizer'), //sizer stores the size of the items

    init = function() {

      // None of these need to be executed synchronously
      setTimeout(function() {
        listen();
        setupFilters();
      }, 100);

      // instantiate the plugin
      $grid.shuffle({
        itemSelector: '[class*="col-"]',
        sizer: $sizer
      });
    },



    // Set up button clicks
    setupFilters = function() {
      var $btns = $filterOptions.children();
      $btns.on('click', function(e) {
        e.preventDefault();
        var $this = $(this),
          isActive = $this.hasClass('active'),
          group = isActive ? 'all' : $this.data('group');

        // Hide current label, show current label in title
        if (!isActive) {
          $('.portfolio-sorting li a').removeClass('active');
        }

        $this.toggleClass('active');

        // Filter elements
        $grid.shuffle('shuffle', group);
      });

      $btns = null;
    },

    // Re layout shuffle when images load. This is only needed
    // below 768 pixels because the .picture-item height is auto and therefore
    // the height of the picture-item is dependent on the image
    // I recommend using imagesloaded to determine when an image is loaded
    // but that doesn't support IE7
    listen = function() {
      var debouncedLayout = $.throttle(300, function() {
        $grid.shuffle('update');
      });

      // Get all images inside shuffle
      $grid.find('img').each(function() {
        var proxyImage;

        // Image already loaded
        if (this.complete && this.naturalWidth !== undefined) {
          return;
        }

        // If none of the checks above matched, simulate loading on detached element.
        proxyImage = new Image();
        $(proxyImage).on('load', function() {
          $(this).off('load');
          debouncedLayout();
        });

        proxyImage.src = this.src;
      });

      // Because this method doesn't seem to be perfect.
      setTimeout(function() {
        debouncedLayout();
      }, 500);
    };

  return {
    init: init
  };
}(jQuery));

$(document).ready(function() {
  shuffleme.init(); //filter portfolio
});

如何从根目录和使用npm脚本永远启动? 我假设初始脚本应该放在

  

的/etc/rc.local

1 个答案:

答案 0 :(得分:1)

要在服务器上部署,您应该尝试pm2

  

Node.js的高级生产流程管理器

相关问题