无论是node.js原生开发,仍是借助express,kora等框架开发node.js的状况下,在对代码作出更新后,都是须要重启已生效咱们的文件的。node
本文记录一次在原生node.js开发的时候,为项目添加热加载。避免一次次手动的重启浪费精力~git
进入正题,咱们须要借助一个node.js的开发工具nodemonexpress
nodemon官方解释以下:npm
nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.json
nodemon does not require any additional changes to your code or method of development. nodemon is a replacement wrapper for node
, to use nodemon
replace the word node
on the command line when executing your script.app
意思大概就是说nodemon是一个node.js的辅助开发工具,具备监听目录文件的做用。并在监听后做出响应。框架
使用:svn
npm install -g nodemon
npm install -s nodemon
安装好之后了,咱们将已经启动的node.js项目关闭,而后经过nodemon命令重启工具
nodemon index.js
这里的index.js就是node.js项目的主入口文件。开发工具
对于express来言的话就是 nodemon bin/www
还有一个比较重要的情景就是,咱们若是说添加了日志文件。或者其余文件。这些文件是不须要进行热加载的。所以。咱们就须要想git上传同样。作出一些过滤
在根目录下常见nodemon.json文件
{ "restartable": "rs", "ignore": [ ".git", ".svn", "logs", "pem", "node_modules/**/node_modules" ], "verbose": true, "execMap": { "js": "node server/index.js" }, "watch": [ ], "env": { "NODE_ENV": "development" }, "ext": "js json" }
restartable就是启动的方式
ignore就是文件的过滤
verbose是否展现详细信息
execMap就是启动的主入口文件
watch能够是监听的文件。
env的话就是环境的配置
在该文件配置ok之后,咱们就不须要nodemon 启动文件了。而是直接输入nodemon命令就好。他会根据配置的json文件进行编译执行。这个的配置和pm2是有很大类似之处的。
若是想要知道更加详细的配置,不防了解一下官方文档:
https://www.npmjs.com/package/nodemon
至此,node.js实现热更新的方法就记录到这里,已记录本身成长的点滴。