博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node---01-nodemon依赖热重载
阅读量:3512 次
发布时间:2019-05-20

本文共 3174 字,大约阅读时间需要 10 分钟。

搞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. // 显示导致重新启动的详细信息  -- 
........... 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

 

 

 

 

 

转载地址:http://evfqj.baihongyu.com/

你可能感兴趣的文章
JavaWeb面经(二):2019.9.16 Synchronized关键字底层原理及作用
查看>>
JavaWeb面试经:redis
查看>>
牛客的AI模拟面试(1)
查看>>
深入浅出MyBatis:MyBatis解析和运行原理
查看>>
Mybatis与Ibatis
查看>>
字节码文件(Class文件)
查看>>
java中的IO流(一)----概述
查看>>
StringBuilder
查看>>
集合,Collection
查看>>
泛型详解
查看>>
泛型实现斗地主
查看>>
List集合
查看>>
ArrayList集合,LinkedList集合,Vector集合
查看>>
HashSet集合
查看>>
并发与并行,线程与进程
查看>>
方法引用,通过对象名引用成员变量
查看>>
常用工具类 Math:数学计算 Random:生成伪随机数 SecureRandom:生成安全的随机数 2020-2-13
查看>>
Java的异常Exception 2020-2-13
查看>>
Java标准库定义的常用异常,自定义异常 2020-2-15
查看>>
Java问题百度/Google记录 2020-2-16
查看>>