如何使用pm2观看和重新加载ExpressJS应用程序

时间:2015-02-13 14:37:31

标签: node.js express pm2

我正在开发ExpressJS应用程序。 我用pm2加载它:

myapp$ pm2 start bin/www

这样做很好,只是添加--watch标志似乎不起作用;每次我更改JS源代码时,我都需要显式重新启动它才能使更改生效:

myapp$ pm2 restart www

我做错了什么?我已使用非ExpressJS应用尝试--watch标记,并且按预期工作。

3 个答案:

答案 0 :(得分:6)

请参阅此solution in Stack Overflow

问题是相对于pm2正在观看的路径,以及它是否相对于执行文件或项目的实际根路径。

答案 1 :(得分:1)

2021 年 2 月

现在情况发生了一些变化。下面从我的项目中给出了一个完整的例子。以下作品:

1 .创建配置文件。文件:ecosystem.config.js

module.exports = {
  apps: [
    {
      name: 'api',
      script: './bin/www', // --------------- our node start script here like index.js

      // ------------------------------------ watch options - begin
      watch: ['../'],
      watch_delay: 1000,
      ignore_watch: ['node_modules'],
      watch_options: {
        followSymlinks: false,
      },
      // ------------------------------------ watch options - end

      env: {
        NODE_ENV: 'development',
        PORT: 3001,
        DEBUG: 'api:*',
        MONGODB_URI:
          'mongodb://localhost:27017/collection1?readPreference=primary&ssl=false',
      },
      env_production: {
        NODE_ENV: 'production',
      },
    },
  ],
  deploy: {
    production: {
        // user: "SSH_USERNAME",
        // host: "SSH_HOSTMACHINE",
    },
  },
};

2 .运行服务器(dev/prod)

pm2 start ecosystem.config.js
pm2 start ecosystem.config.js --env production

3 .更多信息:

https://pm2.keymetrics.io/docs/usage/watch-and-restart/

答案 2 :(得分:0)

您需要为--watch选项指定应用位置

public class AdminUserAndRoles
{
    public static async Task CreateRoles(IServiceProvider serviceProvider, IConfiguration configuration)
    {
        //adding custom roles
        var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
        string[] RoleNames = { "Administrator", "Guest" };
        IdentityResult roleResult;

        foreach (var roleName in RoleNames)
        {
            //creating the roles and seeding them to the database
            var roleExist = await RoleManager.RoleExistsAsync(roleName);
            if (!roleExist)
            {
                roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
            }
        }

        //creating a super user who could maintain the web app
        var AdminUser = new ApplicationUser
        {                
            UserName = configuration.GetSection("AdminSettings")["UserName"],
            Email = configuration.GetSection("AdminSettings")["UserEmail"]
        };

        string UserPassword = configuration.GetSection("AdminSettings")["UserPassword"];
        var _user = await UserManager.FindByEmailAsync(configuration.GetSection("AdminSettings")["UserEmail"]);

        if (_user == null)
        {
            var createPowerUser = await UserManager.CreateAsync(AdminUser, UserPassword);
            if (createPowerUser.Succeeded)
            {
                //here we tie the new user to the "Admin" role 
                await UserManager.AddToRoleAsync(AdminUser, "Administrator");
            }
        }
    }
}