myFirstNodeModule
v2.0.0
Published
回顾: 1、cmd控制台工具 2、环境变量 3、NodeJS的两种安装方式 4、REPL环境 5、Global global.name = name; 6、process中的常用api使用 7、CommonJS规范 8、NODEjs模块化体验
Downloads
2
Readme
回顾: 1、cmd控制台工具 2、环境变量 3、NodeJS的两种安装方式 4、REPL环境 5、Global global.name = name; 6、process中的常用api使用 7、CommonJS规范 8、NODEjs模块化体验
NodeJS模块
在NODEJS中有两种模块: 1、核心模块的作用:node.exe文件中已经编译好了的模块,我们称之为核心模块,它是随着node.exe而发布的 在github.com上的nodejs源代码网址:https://github.com/nodejs/node 按一下t就会自动进入到搜索框:输入搜索内容就会自动查找出对于的文件,方便快速定位
核心模块的优势:执行高效和节省成本
2、文件模块:自己根据业务需求来编写的 .js文件,我们称之为文件模块
NodeJS核心模块
path模块 : 想要在.js中使用path模块,一定要 var path = require('path');
path:表示路径的意思, 在windows中: c:\dd\cc macos和 Linx / Unix /user/loc
c:\dd\cc\index.js
作用: 常用api演示: 1、path.dirname() :获取目录 2、path.basename() :获取文件名.扩展名(我们统称为全名) 3、path.extname() : 获取扩展名 4、path.parse() : 将一个路径转换成一个js对象 5、path.format() :将一个js对象转换成路径 6、join() : 拼接多个路径成一个路径
url模块
想要在.js中使用url模块,一定要 var url = require('url');
作用: 常用api演示: 1、url.parse() :将一个url转换成js对象 2、url.format():将一个urlobj对象转换成url字符串
querystring模块
想要在.js中使用querystring模块,一定要 var querystring = require('querystring'); 主要是用来解析一个url中的参数值的 例如:解析出 https://github.com/?type=delete&id=1&dd=2 中的 type = delete id =1 dd =2
作用: 常用api演示: 1、querystring.parse() : 将一个类似于 id=1&name=ivan的字符串转换成{id:1,name:'ivan'}的js对象 2、querystring.stringify() : 将一个类似于{id:1,name:'ivan'}的js对象的字符串转换成 id=1&name=ivan 3、querytring.escape() : 可以将http://www.baidu.com/dd/?id=1&name=中文 转换成 http%3A%2F%2Fwww.baidu.com%2Fdd%2F%3Fid%3D1%26name%3D%E4%B8%AD%E6%96%87 作用是:防止中文乱码 4、querytring.unescape() :将http%3A%2F%2Fwww.baidu.com%2Fdd%2F%3Fid%3D1%26name%3D%E4%B8%AD%E6%96%87 还原成正常的url
NodeJS文件模块
各一个date对象,将其格式化成yyyy-mm-dd的字符串返回
在Node.js中,一个.js文件就是一个模块
文件模块的两种加载方式
```
```
文件模块的写法演变到包
```
包的概念:
当一个业务需要很多的模块去完成的时候,一般是有一个统一的出口模块来进行所有模块的管理工作,将来其他模块要使用这个业务模块中的
任何一个子模块的时候,就必须要导入统一出口模块即可使用所有的功能,这个封装过程叫做包
包规范:
1、任何一个包都有一个统一的出口
2、任何一个包都有一个package.json配置文件
3、通常情况下,出口文件固定名称是以index.js命名,并且通常这个文件是放在模块文件夹的跟目录和package.json在同一层
当require('某块名称')的时候优先检查package.json mian属性中对于的.js 文件,如果有则执行,否则加载index.js
4、
```
包的概念
使用NPM工具来管理第三方包
NVM : Node Version Manager
NPM : Node Packeage Manager
npm install nvm --save : 在一个包中要依赖另外一个包可以使用,不当做命令行工具来使用 npm install -g nvm :当做工具行命令来使用
可以利用NPM这个工具来将我们自己开发好的包发布到www.npmjs.org中
利用VS Code来进行Nodejs文件的调试
步骤: 1、打开要调试的.js文件,注意点:单个js文件不能直接在vscode中进行调试 2、设置断点:在js代码行号之前点击鼠标左键 3、按F5即可启动调试(如果出现了问题)则点击侧边栏的第4个按钮中的调试 小三角 4、按F5的时候vscode工具会自动创建一个launch.json文件,请将里面的 "program": "${workspaceRoot}/app.js", 中的app.js 修改成 你要调试的 文件名称
在调试过程中, F10:单步跳过 F11:单步调试(进入)
区别: F10都是在当前调试的.js文件中进行执行 (常用) F11:根据代码的调试可能会进入到其他文件中