<repo_name>antbrothers/LearningNode<file_sep>/Chapter01/02_load_albums.js
var http=require('http'),
    fs=require('fs');

function load_albums_list(callback){
    debugger;
    fs.readdir(
        "albums",
        function(err,files){
            debugger;
            if(err){
                callback(err,files);
                return;
            }
            callback(null,files);
        }
    );
}

function handle_incoming_request(req,res){
    console.log("INCOMING REQUEST: " + req.method + " " + req.url);
    debugger;
    load_albums_list(function(err,albums){
        debugger;
        if(err){
            res.writeHead(500,{"Content-Type": "application/json"});
            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");

    });
}
debugger;
var s = http.createServer(handle_incoming_request);

s.listen(8181);<file_sep>/Chapter01/03_test_folder.js
var http = require('http'),
    fs=require('fs');
debugger;
function load_album_list(callback){
    debugger;
    fs.readdir(
        "./node_modules/express/node_modules",
        function (err,files){
            debugger;
            if(err){
                callback(err,files);
                return;
            }
            var only_dirs=[];
            for(var i=0;i < files.length;i++){
                /*fs.stat(
                    "./node_modules/express/node_modules/" + files[i],
                    function (err, stats){
                        if(stats.isDirectory()){
                            only_dirs.push(files[i]);
                        }
                    }
                );*/
                only_dirs.push(files[i]);
            }
            callback(null,only_dirs);
        }
    )
}

function handle_incoming_request(req,res){
    console.log("INCOMING REQUEST:" + req.method +" " + req.url);
    debugger;
    load_album_list(function(err,files){
        debugger;
        if(err){
            res.writeHead(500,{"Content-Type":"application/json"});
            res.end(JSON.stringify(err)+'\n');
            return;
        }
        var out={err:null,data:{albums:files}};
        res.writeHead(200,{"Content-Type":"application/json"});
        res.end(JSON.stringify(out)+"\n")
    });
}
var s = http.createServer(handle_incoming_request);
s.listen(9999);<file_sep>/Chapter01/04_test_folder.js
var http=require('http'),
    fs=require('fs');

function load_album_list(callback){
    fs.readdir(
        'albums',
        function(err,files){
            if(err){
                callback(err);
                return;
            }

            var only_dirs=[];
            (function iterator(index){
                if(index == files.length){
                    callback(null,only_dirs);
                    return;
                }
                fs.stat("albums/"+files[index],
                    function(err,stats){
                        if(err){
                            callback(err);
                            return;
                        }
                        if(stats.isDirectory()){
                            only_dirs.push(files[index]);
                        }
                        iterator(index +1)
                    }
                );
            })(0);
        }
    )
}

function handle_incoming_request(req,res){
    load_album_list(function(err,albums){
        if(err){
            res.writeHead(500, {"Content-Type": "application/json"});
            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(8080);<file_sep>/Chapter01/port.js
var net = require('net');
//检查端口是个被占用
function portIsOccupid(port){
    var server=net.createServer().listen(port);
    server.on('listening',function(){
        server.close(); // 关闭服务
        console.log("The port 【" + port + "】 is available.");
    });
    server.on('error',function(err){
        if(err.code =="EADDRINUSE"){
            console.log("The port【" + port + "】is occupied, please change other port.")
        }
    })
}
portIsOccupid(8585);
