一、为什么要使用nodemon
在编写调试Node.js
项目,修改代码后,需要频繁的手动close掉,然后再重新启动,非常繁琐。现在,我们可以使用nodemon
这个工具,它的作用,是监听代码文件的变动,当代码改变之后,自动重启。
二、怎么使用
1、下载安装(我这里使用npm)
npm install -g nodemon
2、新建一个文件app.js
,并编写下面内容:
var express = require("express")
var app = express()
app.get('/',function(req,res) {
res.send("hello,world")
})
app.listen(3000,function(){
console.log('server is running')
})
3、启动 app.js
传统方法启动时,用node app.js
也是可以的,这里我们用nodemon app.js
nodemon app.js
访问 3000
端口,浏览器中会看到helllo world
这时会看到命令行中内容:
[nodemon] 1.18.11
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
4、修改app.js
文件,我们来看一下是否会自动重启:
app.get('/',function(req,res) {
res.send("hello world!!!!!!!")
})
保存后,再看命令行中内容:
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
我们只需要刷新浏览器,就可以看到改动后的内容。
实际上,我们可以看到,nodemon
其实也是在调用node
命令。