simplehttpserver/simplehttpserver.js
function directoryHTML( res, urldir, pathname, list ) {
    var ulist = [];
    function sendHTML( list ) {
        res.setHeader('Content-Type', 'text/html; charset=utf-8');
        res.send('<!DOCTYPE html>' +
        '<html>\n' +
        '<title>Directory listing for '+htmlsafe(urldir)+'</title>\n' +
        '<body>\n' +
        '<h2>Directory listing for '+htmlsafe(urldir)+'</h2>\n' +
        '<hr><ul>\n' +
        list.join('\n') +
        '</ul><hr>\n' +
        '</body>\n' +
        '</html>');
    }

    if ( !list.length ) {
        // Nothing to resolve
        return sendHTML( ulist );
    }

    // Check for each file if it's a directory or a file
    var q = async.queue(function(item, cb) {
        fs.stat(path.join(pathname, item), function(err, stat) {
            if ( !stat ) cb();
            var link = escape(item);
            item = htmlsafe(item);
            if ( stat.isDirectory() ) {
                ulist.push('<li><a href="'+link+'/">'+item+'/</a></li>')
            } else {
                ulist.push('<li><a href="'+link+'">'+item+'</a></li>')
            }
            cb();
        });
    }, 4);
    list.forEach(function(item) {
        q.push(item);
    });
    q.drain = function() {
        // Finished checking files, send the response
        sendHTML(ulist);
    };
}
function directoryHTML( res, urldir, pathname, list ) {
    var ulist = [];
    function sendHTML( list ) {
        res.setHeader('Content-Type', 'text/html; charset=utf-8');
        res.send('<!DOCTYPE html>' +
        '<html>\n' +
        '<title>Directory listing for '+htmlsafe(urldir)+'</title>\n' +
        '<body>\n' +
        '<h2>Directory listing for '+htmlsafe(urldir)+'</h2>\n' +
        '<hr><ul>\n' +
        list.join('\n') +
        '</ul><hr>\n' +
        '</body>\n' +
        '</html>');
    }

    if ( !list.length ) {
        // Nothing to resolve
        return sendHTML( ulist );
    }

    // Check for each file if it's a directory or a file
    var q = async.queue(function(item, cb) {
        fs.stat(path.join(pathname, item), function(err, stat) {
            if ( !stat ) cb();
            var link = escape(item);
            item = htmlsafe(item);
            if ( stat.isDirectory() ) {
                ulist.push('<li><a href="'+link+'/">'+item+'/</a></li>')
            } else {
                ulist.push('<li><a href="'+link+'">'+item+'</a></li>')
            }
            cb();
        });
    }, 4);
    list.forEach(function(item) {
        q.push(item);
    });
    q.drain = function() {
        // Finished checking files, send the response
        sendHTML(ulist);
    };
}
function directoryHTML ( res , urldir , pathname , list ) { var ulist = [ ] ; function sendHTML ( list ) { res . setHeader ( 'Content-Type' , 'text/html; charset=utf-8' ) ; res . send ( '<!DOCTYPE html>' + '<html>\n' + '<title>Directory listing for ' + htmlsafe ( urldir ) + '</title>\n' + '<body>\n' + '<h2>Directory listing for ' + htmlsafe ( urldir ) + '</h2>\n' + '<hr><ul>\n' + list . join ( '\n' ) + '</ul><hr>\n' + '</body>\n' + '</html>' ) ; } if ( ! list . length ) { // Nothing to resolve return sendHTML ( ulist ) ; } // Check for each file if it's a directory or a file var q = async . queue ( function ( item , cb ) { fs . stat ( path . join ( pathname , item ) , function ( err , stat ) { if ( ! stat ) cb ( ) ; var link = escape ( item ) ; item = htmlsafe ( item ) ; if ( stat . isDirectory ( ) ) { ulist . push ( '<li><a href="' + link + '/">' + item + '/</a></li>' ) } else { ulist . push ( '<li><a href="' + link + '">' + item + '</a></li>' ) } cb ( ) ; } ) ; } , 4 ) ; list . forEach ( function ( item ) { q . push ( item ) ; } ) ; q . drain = function ( ) { // Finished checking files, send the response sendHTML ( ulist ) ; } ; }
Reads directory content and builds HTML response
Reads directory content and builds HTML response