node自动重启工具:nodemon

1,002 阅读1分钟

我们在使用nodejs开发服务端时,修改完代码需要重新启动才能使代码生效,此时我们可以使用nodemon来监听文件变化后自动重启服务。

使用步骤:

第一步,安装

npm i nodemon -g

第二步,编写指令

可以在package.js 文件中编写nodemon的启动命令,如下

{
  "name": "blog-1",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "server":"nodemon ./bin/index.js"
  },
  "author": "",
  "license": "ISC"
}

当然,开启的./bin/index.js 需要有启动服务器的代码,如下:

var http = require("http");
const serverHandle = require('../app.js');

http.createServer(function (req, res) {
    serverHandle(req, res);
}).listen(8080);
console.log("localhost:8080");

第三步,在终端中启动命令

image.png

OK了,再次修改代码时nodemon会帮你自动重启!