前几天说以了下Node.js的模块化,这里再次说一下为何Node.js中须要模块化html
JavaScript的局限性在后台开发语言中,好比Java、C#。他们都是隐含模块化的,Node.js默认帮咱们提供了模块化这种机制。 在服务器端,咱们想要使用底层的一些功能须要导入一些“包”来对其操做,好比操做文件、网络须要导入对应的包。其它语言中都是基于类来实现的模块化的思想,使用类来组织文件和文件之间的关联。 而Node.js中使用的是JavaScript语言,ECMAScript仅仅规定了基本的语法的书写,并无规定文件之间 关联,也就是说每一个js文件之间是独立的,Node.js已经帮咱们实现了js文件之间的关联(模块化) Node.js中的模块化是基于CommonJS规范的
那么接下来看一下核心模块node
今天接触到的核心模块:git
path模块github
basename() 获取文件名+后缀web
path.basename("/foo/hello/world/123.html") //第二个参数,去掉获取的文件名中的相同部分 path.basename("c:/foo/hello/world/123.html",".html")
dirname() 获取目录数据库
path.dirname("/foo/hello/world/123.html")
extname() 获取文件的扩展名(尾缀名)windows
path.extname("/foo/hello/world/123.html")
join() 合并路径服务器
var p1 = "c://abc/xyz"; var p2 = "/123/456"; console.log(path.join(p1,p2));
parse() 把路径转换为一个对象网络
path.parse("c:\\home\\hello\\world\\123.html") { root: 'c:/', dir: 'c://home/hello/world', base: '123.html', ext: '.html', name: '123' }
format() 把一个路径对象转换成一个路径字符串模块化
var obj = { root: 'c:\\', dir: 'c:\\home\\hello\\world', base: '123.html', ext: '.html', name: '123' } console.log(path.format(obj));
delimiter 环境变量的分隔符,能够跨平台 windows下是; 其它平台 :
path.sep 路径的分隔符 windows下是\ 其它下是/
isAbsolute() 是不是绝对路径
var uri = "http://www.baidu.com:8080/images/1.jpgversion=1.0&time=1123#abcd"; console.log(url.parse(uri));
format() 把路径对象转换成字符串
var obj = { protocol: 'http:', slashes: true, auth: null, host: 'www.baidu.com:8080', port: '8080', hostname: 'www.baidu.com', hash: '#abcd', search: '?version=1.0&time=1123', query: 'version=1.0&time=1123', pathname: '/images/1.jpg', path: '/images/1.jpg?version=1.0&time=1123', href: 'http://www.baidu.com:8080/images/1.jpg?version=1.0&time=1123#abcd' }; var str = url.format(obj); console.log(str);
querystring模块
parse() 把参数字符串解析成对象
var obj = querystring.parse("version=1.0&time=123"); console.log(obj);
stringify() 把一个对象转换成一个字符串
escape() url进行编码
unescape() url进行解码
----------------------------------------------------分割线-----------------------------------------------
那么核心模块存在哪里呢,