当代码发生更改时,在 Vagrant 上自动启动 Node.js 应用程序 [英] Auto-starting Node.js application on Vagrant when there are changes to code

查看:36
本文介绍了当代码发生更改时,在 Vagrant 上自动启动 Node.js 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Vagrant 机器上运行 Node.js 应用程序,并希望 Node.js 应用程序在主机发生任何变化时重新启动.

I'm running a Node.js application on a Vagrant box and want the Node.js application to restart when anything changes on the host machine.

但我无法使用 nodemon.它不会及时发现变化.当我使用 -L 标志时,它在来宾机器上消耗了太多 CPU.这些是目前众所周知的问题.

But I couldn't use nodemon. It doesn't pick up the changes in time. When I use the -L flag, it consumes too much CPU on the guest machine. These are currently well-known issues.

我也试过 Vagrant 的 rsync-auto,但它由于某种原因没有足够的响应.

I've also tried Vagrant's rsync-auto, but it wasn't responsive enough for some reason.

有解决方法吗?也许我可以在主机上应用一些东西?

Is there a workaround? Perhaps something I can apply on the host machine?

推荐答案

您似乎可以在来宾计算机上尝试其他工具作为 nodemon 的替代品,但在主机上观察文件更改将是最快的.

There seem to be other tools you can try on the guest machine as alternatives to nodemon, but watching for file changes will be the fastest on the host machine.

这里有一个对我有用的解决方法:

Here's a workaround that has worked for me:

这个要点:在主机上使用观察者并向来宾机器发出 SSH 命令以重新启动应用程序.

This gist: Use a watcher on the host machine and issue an SSH command to the guest machine to restart the application.

这是食谱(您可以使用任何其他您喜欢的工具):

Here's a recipe (you can use any other tools you like):

首先,获取pm2:

npm install -g pm2

并通过 pm2 启动您的 Node.js 应用程序:

And launch your Node.js application via pm2:

pm2 app/server.js

然后抓取Gulp:

npm install -g gulp-cli
npm install --save-dev gulp

还有gulp-shell:

npm install --save-dev gulp-shell

然后创建一个 gulpfile.js:

Then create a gulpfile.js:

var gulp = require('gulp');
var shell = require('gulp-shell');

gulp.task('server:watch', function () {
  gulp.watch('app/**/*.js', [ 'server:restart' ]);
});

gulp.task('server:restart', shell.task([ 'vagrant ssh -- pm2 restart server' ]));

你已经完成了.开始监视文件并在文件更改时自动启动服务器:

You're done. To start watching the files and automatically start the server on file changes:

gulp server:watch

这篇关于当代码发生更改时,在 Vagrant 上自动启动 Node.js 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆