events
- EventEmitter
事件发射与事件监听
var emitter = new events.EventEmitter();
emitter.on('someEvent',function(arg1,arg2){
console.log('listenr1',arg1,arg2)
})
emitter.on('someEvent',function(arg1,arg2){
console.log('listenr2',arg1,arg2)
})
emitter.emit('someEvent','a',1)
//listenr1 a 1
//listenr2 a 1
.on(event,listener)
.emit(event,arg..)
.once(event,listener)
一个监听器只触发一次随后立即解除
.removeListener(event,listener)
.removeAllListener(event)
fs
.readFile(filename,[encoding],[callback(err,data)])
异步
.readFileSync(filename,[encoding])
同步
.resdOpen(path,flags,[mode],[callback(err,fd)])
.read(...)
是open的更底层接口,手动管理缓冲区和文件指针
HTTP
服务器
http.server
var server = new http.Server()
server.on('request',function(req,res){
...
})
server.listen(3000)
//request 客户端请求时
//connection TCP连接时
//close 服务器关闭时
request=>createServer([requestListener])
http.serveRequest
HTTP请求的信息