node---01-nodemon依赖热重载

搞vue.js搞久了,下意识依赖热重载,ctrl+s保存!!页面自动呈现最新状态!!简直爽!!

导致搞node.js的时候,ctrl+s保存,一脸期待的等待最新状态,结果...哎哎哎??哦。。这是node.js,需要输入命令

“node [启动文件]”才可以重新编译启动(嫌弃脸~)

于是乎,去某度搜索了一波,发现了这玩意儿——nodemon(自动重启模块)

nodemon用来监视node.js应用程序中的任何更改并自动重启服务,非常适合用在开发环境中。

nodemon将监视启动目录中的文件,如果有任何文件更改,nodemon将自动重新启动node应用程序。

nodemon不需要对代码或开发方式进行任何更改。 nodemon只是简单的包装你的node应用程序,并监控任何已经改变的文件。nodemon只是node的替换包,只是在运行脚本时将其替换命令行上的node。

安装nodemon需要输入命令“nodemon [启动文件]”。

过程:

1、安装依赖

全局安装:

npm install -g nodemon

本地安装:

npm install nodemon --save-dev

安装完后,可以通过nodemon -v查看版本来判断是否安装成功

2、启动nodemon

输入命令行:

nodemon

输出:

[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server running on URL http://127.0.0.1:5000

3、关于CLI选项

输入命令行:

nodemon -h

输出:

Options:

  --config file ............ alternate nodemon.json config file to use // 备用nodemon.json配置文件使用
  -e, --ext ................ extensions to look for, ie. js,jade,hbs. // 监控指定后缀名的文件
  -x, --exec app ........... execute script with "app", ie. -x "python -v". // 执行的命令
  -w, --watch dir........... watch directory "dir" or files. use once for // 监控文件夹
                             each directory or file to watch.
  -i, --ignore ............. ignore specific files or directories. // 忽略特定的文件或目录
  -V, --verbose ............ show detail on what is causing restarts. // 显示导致重新启动的详细信息
  -- <your args> ........... to tell nodemon stop slurping arguments. // 告诉nodemon停止参数

  Note: if the script is omitted, nodemon will try to read "main" from
  package.json and without a nodemon.json, nodemon will monitor .js, .mjs, .coffee,
  and .litcoffee by default.

  For advanced nodemon configuration use nodemon.json: nodemon --help config
  See also the sample: https://github.com/remy/nodemon/wiki/Sample-nodemon.json

  Examples:

  $ nodemon server.js
  $ nodemon -w ../foo server.js apparg1 apparg2
  $ nodemon --exec python app.py
  $ nodemon --exec "make build" -e "styl hbs"
  $ nodemon app.js -- --config # pass config to app.js

这个时候,我观察到了“--config file”

原来,除了通过命令去修改nodemon的配置外,我可以另外创建一个文件作为nodemon的配置文件

4、配置nodemon.json文件

在跟目录下创建nodemon.json文件

{
  "watch": ["src"], // 监听src目录下文件变化
  "ext": "ts", // 监控指定后缀名的文件
  "ignore": ["src/**/*.spec.ts"], // 忽略的文件名后缀或文件夹
  "exec": "node" // 当监控到变化时,自动执行的命令
}

输入命令行:

nodemon --config nodemon

重启服务会发现输出的配置发生了变化:

[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server running on URL http://127.0.0.1:5000

5、小拓展

修改package.json配置:

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "server": "nodemon server.js"
  }

命令行中输入npm run server,相当于执行nodemon。

测试:

let express = require('express');
const app = express();
const port = process.env.PORT || 5000;

app.get("/", (req, res) => {
    res.send("Hello World!");
})

app.listen(port, () => {
    console.log(`Server running on URL http://127.0.0.1:${port}`);
})

终端输入:

npm run server

会发现可能会报下面错误:

nodemon : 无法加载文件 C:\Users\19336\AppData\Roaming\npm\nodemon.ps1,因为在此系统上禁止运行脚本。

解决办法:

1. 管理员身份打开powerShell

2. 输入set-ExecutionPolicy RemoteSigned 

3. 选择Y 或者A ,就好了

服务器开启成功:npm run server

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值