var http = require("http");//负责创建web服务器,处理http相关任务
fs = require('fs');
function load_album_list (callback){
console.log(2);
fs.readdir(
'img','utf-8',function(err,files){
if(err){
callback(err);
return;
}
callback(null,files);
}
);
}
function handle_incoming_request(req,res){
console.log(1);
res.setCharacterEncoding("utf-8");
console.log("incoming request:" + req.method+ "" +req.url);
load_album_list(function(err,albums){
if(err){
res.writeHead(503,{"Content-Type":"application/json;charset=utf-8"});
res.end(JSON.stringify(err) + "\n");
return;
}
var out = {
error:null,
data:{albums:albums}
};
res.writeHead(200,{"Content-Type":"application/json"});
res.end("封测人"+JSON.stringify(out) + "\n");
});
}
var s = http.createServer(handle_incoming_request);
s.listen(3000); |