node.js 在代码更改时自动重新加载 Sails.js 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18687818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto reloading a Sails.js app on code changes?
提问by subblue
Currently is seems that for any code change in a sails.js app you have to manually stop the sails server and run sails liftagain before you can see the changes.
目前看来,对于sails.js 应用程序中的任何代码更改,您必须手动停止sails 服务器并sails lift再次运行,然后才能看到更改。
I was wondering if there is any way when running in development mode to automatically restart the sails server when it detects a code change?
我想知道在开发模式下运行时是否有任何方法可以在检测到代码更改时自动重新启动 Sails 服务器?
采纳答案by Sandro Munda
You have to use a watcher like forever, nodemon, or something else...
您必须使用诸如永远、nodemon或其他东西之类的观察者...
Example
例子
Install foreverby running:
sudo npm install -g foreverRun it:
forever -w start app.js
通过运行永久安装:
sudo npm install -g forever运行:
forever -w start app.js
To avoid infinite restart because Sails writes into .tmpfolder, you can create a .foreverignorefile into your project directory and put this content inside:
为避免由于 Sails 写入.tmp文件夹而导致无限重启,您可以.foreverignore在项目目录中创建一个文件并将此内容放入其中:
**/.tmp/**
**/views/**
**/assets/**
See the issue on GitHub: Forever restarting because of /.tmp.
请参阅 GitHub 上的问题: Forever restarting because of /.tmp。
回答by Vitalii Maslianok
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.
只需像往常一样提升您的应用程序,当您添加/更改/删除模型或控制器文件时,所有控制器和模型都将重新加载,而无需降低/重新提升应用程序。
回答by woss
For example with nodemonto watch api and config directories
例如nodemon观看 api 和 config 目录
.nodemonignorecontents
.nodemonignore内容
views/*
.tmp/*
.git/*
Run the command after creating .nodemonignore
创建 .nodemonignore 后运行命令
$> nodemon -w api -w config
Example for supervisor to ignore 3 directories
主管忽略 3 个目录的示例
$> supervisor -i .tmp,.git,views app.js
回答by Ben Fried
If you're using Sails 0.11, you can install this hook to automatically reload when you change models or controllers (views do not require reloading):
如果你使用的是 Sails 0.11,你可以安装这个钩子来在你改变模型或控制器时自动重新加载(视图不需要重新加载):
npm install sails-hook-autoreload
回答by josec89
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:
我遇到了同样的问题,我已经使用 grunt-watch 和 grunt-forever 和sails@beta 任务解决了这个问题。结果是 4 个 grunt 命令:
UPDATE: tasks are available in the current sails version (it's no longer beta :>)
更新:任务在当前的风帆版本中可用(它不再是测试版:>)
- startStarts the server
- stopStops the server
- restartRestarts the server
- startWatchStarts the server and waits for changes to restart it (using grunt-watch). This is probably your solution, but the other commands are also useful.
- start启动服务器
- 停止停止服务器
- restart重新启动服务器
- startWatch启动服务器并等待更改以重新启动它(使用 grunt-watch)。这可能是您的解决方案,但其他命令也很有用。
Here's the code - I'm using sails@beta, which includes a tasksdirectory, I don't know if this is included in previous versions:
这是代码 - 我使用的是sails@beta,其中包含一个任务目录,我不知道以前的版本中是否包含它:
First of all you have to install forever in your sails directory:
npm install grunt-forever --save-devtasks/config/forever.jsConfigure 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.jsRegister 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' ]); };
首先,你必须在你的sails目录中永久安装:
npm install grunt-forever --save-devtasks/config/forever.js 配置永久任务。
module.exports = function(grunt) { grunt.config.set('forever', { server: { options: { index: 'app.js', logDir: 'logs' } } }); grunt.loadNpmTasks('grunt-forever'); };tasks/config/watch.js ( edit)编辑监视任务以添加新规则
// api and assets default rules , server: { // Server files to watch: files: [ 'api/**/*', 'config/**/*' ], // Restart server tasks: ['forever:server:restart'] }tasks/register/watchForever.js注册您的自定义任务(此文件可以重命名为您想要的任何名称)
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!
希望这有帮助!
回答by Mujtaba Kably
install nodemonglobally or locally.
nodemon全局或本地安装。
npm install nodemon --save
npm install nodemon -g
install sailslocally in you project as follows
sails在您的项目中本地安装,如下所示
npm install sails --save
then change package.json
然后改变 package.json
from
从
"scripts": {
"debug": "node debug app.js",
"start": "node app.js"
},
to
到
"scripts": {
"debug": "node debug app.js",
"start": "node app.js",
"dev": "export NODE_ENV=development && nodemon --ignore 'tmp/*' app.js && exit 0"
},
then
然后
npm run dev
回答by BINFAS BINU
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
回答by Nick F
For anyone coming to this question now, it seems that this is no longer necessary - an application launched with sails liftwill have a grunt watch task running, and code changes will be visible without a restart.
对于现在遇到这个问题的任何人来说,这似乎不再需要 - 启动的应用程序sails lift将运行 grunt watch 任务,并且无需重新启动即可看到代码更改。
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)
一开始我没有意识到这发生了,因为没有任何迹象表明控制台中发生了什么,但它似乎无需重启就可以工作(我使用的是 Sails 0.11)

