nodejs API笔记

一、URL

  涉及到的方法

    1、parse():解析地址

    2、format():生成地址

    3、resolve(from,to):组合成地址

  举例说明:

url.parse('http://baidu.com:8010/list?from=lk&lk=ooo#dd')
输出结果:
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'baidu.com:8010',
  port: '8010',
  hostname: 'baidu.com',
  hash: '#dd',
  search: '?from=lk&lk=ooo',
  query: 'from=lk&lk=ooo',  //解析成字符串
  pathname: '/list',
  path: '/list?from=lk&lk=ooo',
  href: 'http://baidu.com:8010/list?from=lk&lk=ooo#dd' }

url.parse('http://baidu.com:8010/list?from=lk&lk=ooo#dd',true)
输出结果:
{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'baidu.com:8010',
  port: '8010',
  hostname: 'baidu.com',
  hash: '#dd',
  search: '?from=lk&lk=ooo',
  query: { from: 'lk', lk: 'ooo' },  //解析成对象
  pathname: '/list',
  path: '/list?from=lk&lk=ooo',
  href: 'http://baidu.com:8010/list?from=lk&lk=ooo#dd' }

  

url.resolve('http://baidu.com','/list')
输出结果:
'http://baidu.com/list'

二、querystring

  解析query

    1、序列化函数stringify()

querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='

querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
// returns
'foo:bar;baz:qux'

   2、反序列化函数parse()

querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }

querystring.parse('foo:bar;baz:qux;baz:quux;corge:',';',':')

{ foo: 'bar',
  baz: [ 'qux', 'quux' ],
  corge: '' }

   3、转义和反转义escape()和unescape()

querystring.escape('哈哈')
输出结果:
'%E5%93%88%E5%93%88'
querystring.unescape('%E5%93%88%E5%93%88')
输出结果:
'哈哈'

三、HTTP模块

  1、HTTP事件回调

    什么是回调:nodejs按顺序执行异步逻辑时,一般采用后续传递,即将后续逻辑封装在回调函数中,作为起始函数的参数

  举例说明:

function learn(something){
	console.log(something)
}

function we(callback, something){
	something += ' is cool!'
	callback(something)
}

we(learn, 'Nodejs')

  2、什么是同步和异步

var c = 0
function print() {
	console.log(c)
}

function plus (){
	
	setTimeout(function(){
		c+=1
	},1000)  //延时1秒执行
}
plus()
print()
输出结果是 0


改进代码(回调的形式)
var c = 0
function print() {
	console.log(c)
}

function plus (callback){
	
	setTimeout(function(){
		c+=1
		callback()
	},1000)
}
plus(print)
输出结果为1

  3、IO
    文件和数据库的读取和写入。

  4、Nodejs核心思想

    非阻塞、单线程、事件驱动 

  5、HTTP源码解读
  5.1、什么是作用域
    分为局部和全局作用于(函数、变量)
  5.2、什么是上下文
    主要是指this方法名,this指向函数拥有者,只在函数内部使用。
    举例说明:

var pet = {
	words: '...',
	speak: function() {
		console.log(this.words)
		console.log(this === pet)
	}
}

pet.speak()

输出:
...
true

 

注意:js中类的可以用function()实现,当然也可以用pet = {}来实现
function pet(words){
	this.words = words
	this.speak = function(){
		console.log(this.words)
		console.log(this)
	}
}

var cat = new pet('CAT')
cat.speak()
输出结果:
CAT
{ words: 'CAT', speak: [Function] }

  5.3call的使用

     call改变了上下文,将this指向了dog

pet = {
	words: '...',
	speak: function(say){
		console.log(say+" "+this.words)
	}
}

var dog = {
	words: 'Wang'
}
pet.speak.call(dog,'Speak')

输出结果:
Speak Wang

     利用call可以实现继承:

    举例说明:

function pet(words){
	this.words = words
	this.speak = function(){
		console.log(this.words)
	}
}


function Dog(words){
	pet.call(this,words)//将pet的this指向Dog
}

var dog = new Dog('Wang')
dog.speak();

输出结果:
Wang

  5.4HTTP性能测试:

   利用Apache ab测试
    $ ab -n1000 -c100 http://127.0.0.1:3000
    -n1000指总的请求数是1000
    -c100指并发性是100
  还有其他参数
    -t测试所进行总时间
    -p post文件
    -w以html表格的形式输出结果

posted @ 2015-10-10 20:41  likai_liche  阅读(538)  评论(0编辑  收藏  举报