pomelo版本: 2.2.5 , 编辑脚本 app.js 加入以下代码node
//全局配置 app.configure('production|development', function() { //让全部服务器 都支持 handle 和remote 热更新 let serverConfig = { 'reloadHandlers':true, 'reloadRemotes':true, }; app.set('serverConfig',serverConfig); });
//监听 handler var watchHandlers = function(app, handlerMap) { var p = pathUtil.getHandlerPath(app.getBase(), app.serverType); if (!!p){ fs.watch(p, function(event, name) { if(event === 'change') { handlerMap[app.serverType] = Loader.load(p, app); } }); } };
注意: 在 remote和handler 文件里不要保存局部数据,不然刷新之后会丢失.git
参考项目 https://github.com/NetEase/treasuresgithub
{ "name": "bearcat", "scan": "app", "beans": [] }
scan 就是要检测的目录 .json
//BEARCAT_HOT 必定要配置成 on //BEARCAT_LOGGER 若是不关闭,则pomelo的日志输出 会将全部的日志都输出到 pomelo-undefined.log 里面. var contextPath = require.resolve('./context.json'); bearcat.createApp([contextPath], { BEARCAT_HOT: 'on',// 开启热更新,若是是off 那么不会热更新 BEARCAT_LOGGER: 'off',//setup 'off' to turn off bearcat logger configuration, } ); // 启动APP // app.start(); bearcat.start(function() { app.set('bearcat', bearcat); // start app app.start(); });
经过上面简单的操做,bearcat 就已经配置好了.试一试修改文件,控制台就会有提示bearcat从新加载.服务器
Handler.prototype.enter = function (msg, session, next) { //这行若是写在函数外面,则不能热更新,每次修改文件要生效,都必须从新加载文件 var Student = require('./student'); var _stu = new Student(); _stu.say(); next(null,0); return; }
var Student = function(){ } Student.prototype.say = function(){ console.log("i am old say"); console.log("i am old say"); console.log("i am old say"); console.log("i am old say"); } module.exports = function(){ return new Student(); }
测试过程:session
这行若是写在函数外面,则不能热更新,每次修改文件要生效,都必须从新加载文件
var Student = require('./student');app
说明 bearcat 更新主要是更新 新建立的对象! 之前的对象是使用之前的代码,这种状况是更新不了的!函数