var ht=require(‘http’);
var url=require(‘url’);
var items=[];//相当于将数据存到内存中
var count=0
ht.createServer(function(req,res){
count++;
console.log(“收到请求”);
switch(req.method){
case 'POST’:
var item=’’;
req.setEncoding(‘utf-8’);
req.on('data’,function(chunk){
item+=chunk;
console.log('parsed’,chunk);
});
req.on('end’,function(){
items.push(item);
console.log(‘done parsing’);
});
res.end(“请求的次数为:第"+count+"次”);
break;
case ‘GET’:
items.forEach(function(item,i){
res.write(i+’) ‘+item+’\n’);
});
var body=items.map(function(v,k){return k.toString()+") "+v;})
res.setHeader(‘Content-Type’,’text/plain;charset="utf-8"’)
res.end(“请求的次数为:第"+count+"次”);
break;
case 'DELETE’:
break;
case 'PUT’:
break;
}
console.log(“这是第",count,"次请求!”);
}).listen(3000);