A simple Http File Server written in Node.js 0.4

Wednesday, 23 March 2011 17:50 by myro

A really interesting project that caught my attention lately is Node.js which is an evented I/O based on google's V8 JavaScript engine.
In few hours I have created a really simple http server in Node.js 0.4 which allows you to browse and download any server's file directly from your browser. First of all let's take a look at the code... if you want to run it just copy and paste it into a txt file and name it HttpFileSystem.js

var http = require('http');
var fs = require('fs');

var httpserver = http.createServer(onRequest);
httpserver.listen(8124, "127.0.0.1",serverCreated_callback());

function onRequest(request, response){
    var pureUrl = request.url;
    if (pureUrl == '/favicon.ico')
        return;
    var candidateQS = require('url').parse(request.url)['query'];
    pureUrl = UrlToFsPath(pureUrl);
   
    console.log(pureUrl + ' is requested');
   
    try{
         if (candidateQS != null) {
            // a file is requested
            candidateQS = candidateQS.substr(5,candidateQS.lenght);
            var fileData = fs.readFileSync(candidateQS);
            response.writeHead(200);
            response.write(fileData);
            response.end();
        }
    else {
            var htmlOutput = FormatHtmlFileList(pureUrl);
            response.writeHead(200, {
                'Content-Type': 'text/html'
            });
            response.end(htmlOutput, 'utf8');
        }
       
    }
    catch(error) {
        response.writeHead(404, {
                'Content-Type': 'text/html'
            });
        response.end();
       
    }
   
   
   
}

function serverCreated_callback(){
  console.log('HttpFileSystem Server running at http://127.0.0.1:8124/');
}

function UrlToFsPath(pureUrl){
    if( pureUrl.substr(-1) != "/" ){
        pureUrl += "/";
    }
    return unescape(pureUrl.replace(/\+/g, " "));;     
}

function FormatHtmlTableRow(absolutePath, filename){
     var fileStats = fs.statSync(absolutePath);
     var htmlRow;
     if(fileStats.isFile()){
        htmlRow = "<tr><td>[-]</td><td style='width:300px'><a href='/?file=" + absolutePath + "'>"
        + filename + "</a> </td><td>" + fileStats['ctime'] + "</td><td>"+ fileStats['size'] +"</td>";
     }
     else{
        htmlRow = "<tr><td>[+]</td><td style='width:300px'><a href='" + absolutePath + "'> "
        + filename + "</a> </td><td>" + fileStats['ctime'] + "</td>";
     }
     return htmlRow;
}

function FormatHtmlFileList(pureUrl){
    var htmlOutput = '<h3>Folder: ' + pureUrl + '</h3>';
    htmlOutput +="<a href='"+ pureUrl +"' >Go back</a>";
    htmlOutput +='<table>';
    htmlOutput +="<tr><td> </td><td style='width:300px'> Name </td><td>Create Date</td><td>Size</td>";
    var directories = fs.readdirSync(pureUrl);
    for (var i in directories) {
        var absolutePath =  pureUrl + directories[i]
        htmlOutput += FormatHtmlTableRow(absolutePath, directories[i]);
    }
    htmlOutput +='</table>';
    return htmlOutput;
   
}

After installing Node.js into your computer you can run this code by tiping into your terminal:

myo@debian:~$ ./node HttpFileSystem.js
HttpFileSystem Server running at http://127.0.0.1:8124/
 

And as you can see a message will inform you that an http server is running under your localhost's 8124 port.
Open up you browser and point to http://127.0.0.1:8124/

Now you see what's in your root..

 

...browse directories..  


... view your local images...
 
 
... view text files...
 
 
... and eventually download files...
 
 
Please note that due to actual V8 limitations you cannot download huge files... until V8 fixes this lack
 
 

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   NodeJs
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.