1nodejs 常用网站
·nodejs.org(nodejs 官网)
·www.npmjs.com
·github.com
·stackoverflow.com
1安装nodejs
2.模块与包管理
nodejs 是基于Commonjs 规范,每个模块变量不会相互污染。
模块的分类:
·核心模块 :http,fs,path
·文件模块:var util = require('./util.js');
·第三方模块:var promise = require('bluebird') 通过npm 安装
Node.js API
1.URL
URI :统一资源标识符
URL:统一资源的定位fu。
URL是URI的子集。
URL:
(1)parse
url.parse('http://www.imooc.com/course/list?from=scott&&course=node');
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.imooc.com',
port: null,//默认8080
hostname: 'www.imooc.com',
hash: null,
search: '?from=scott&&course=node',
query: 'from=scott&&course=node',
pathname: '/course/list',
path: '/course/list?from=scott&&course=node',
href: 'http://www.imooc.com/course/list?from=scott&&course=node' }
(2)formate(与parse是相反的过程)
url.format({
... protocol: 'http:',
... slashes: true,
... auth: null,
... host: 'www.imooc.com',
... port: null,
... hostname: 'www.imooc.com',
... hash: null,
... search: '?teacher=mocco&&course=node',
... query: 'teacher=mocco&&course=node',
... pathname: '/course/list',
... path: '/course/list?teacher=mocco&&course=node',
... href: 'http://www.imooc.com/course/list?teacher=mocco&&course=node' });
'http://www.imooc.com/course/list?teacher=mocco&&course=node'//结果
(3)resolve()
url.resolve('http://www.imooc.com/','/course/list');
'http://www.imooc.com/course/list'//结果
(4)parse(url,queryString)
url.parse('http://www.imooc.com/course/list?teacher=mocco&&course=node',true)
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.imooc.com',
port: null,
hostname: 'www.imooc.com',
hash: null,
search: '?teacher=mocco&&course=node',
query: { teacher: 'mocco', '': '', course: 'node' },
//由原来的字符串,变为json对象
pathname: '/course/list',
path: '/course/list?teacher=mocco&&course=node',
href: 'http://www.imooc.com/course/list?teacher=mocco&&course=node' }
(5)parse(url,queryString,slashesDenoteHost)
slashesDenoteHost默认值为false,当为ture时,在传入不知道的传输协议后仍然可以正确解析URL
url.parse('//www.imooc.com/course/list?teacher=mocco&&course=node',true)
Url {
protocol: null,
slashes: null,
auth: null,
host: null,//false
port: null,
hostname: null, //??不正确
hash: null,
search: '?teacher=mocco&&course=node',
query: { teacher: 'mocco', '': '', course: 'node' },
pathname: '//www.imooc.com/course/list',
path: '//www.imooc.com/course/list?teacher=mocco&&course=node',
href: '//www.imooc.com/course/list?teacher=mocco&&course=node' }
url.parse('//www.imooc.com/course/list?teacher=mocco&&course=node',true,true)
Url {
protocol: null,
slashes: true,
auth: null,
host: 'www.imooc.com',//true
port: null,
hostname: 'www.imooc.com',//true
hash: null,
search: '?teacher=mocco&&course=node',
query: { teacher: 'mocco', '': '', course: 'node' },
pathname: '/course/list',
path: '/course/list?teacher=mocco&&course=node',
href: '//www.imooc.com/course/list?teacher=mocco&&course=node' }
2.queryString
(1)stringify(json,[tag1[,tag2]])对象转为查询字符串
tag1:表示不同对象之间的连接符,默认为&
tag2:表示属性赋值符号,默认为 =
querystring.stringify({name:'lxyhello',course:['node','jade']},'#',':');
'name:lxyhello#course:node#course:jade'
(2)parse(str,tag1,tag2);和stringify 作用相反
querystring.parse('name:lxyhello#course:node#course:jade',"#",":");
{ name: 'lxyhello', course: [ 'node', 'jade' ] }
(3)escape(str); 转译,转化成字符串。
(4)unescape(str);将escape(str)的字符串转成原始字符串
> querystring.escape('');
'%3Chello%3E'
> querystring.unescape('%3Chello%3E');
''
3http
(1)什么是http?一种通信协议
流程:
(1)http 客户端发起请求,创建端口
(2)http服务器在端口监听客户端请求。
(3)http服务器向客户端返回状态和内容。
(4)http