Simple static web server with node.js

A simple static server utilizing nodejs:

Just create a file named server.js with the code below and put it in the folder you want to serve static content from. Then navigate to that folder and just run node server.js

You should be able to browse your folder under http://localhost:3000 now.

Hint: index.html files are also recognized as entry points for folders.


var http  = require("http"),
    url   = require("url"),
    path  = require("path"),
    fs    = require("fs"),
    port  = process.argv[2] || 3000,

    mimeTypes = {
      'asc'   : 'text/plain',
      'au'    : 'audio/basic',
      'avi'   : 'video/x-msvideo',
      'bin'   : 'application/octet-stream',
      'class' : 'application/octet-stream',
      'css'   : 'text/css',
      'csv'   : 'application/vnd.ms-excel',
      'doc'   : 'application/msword',
      'dll'   : 'application/octet-stream',
      'dvi'   : 'application/x-dvi',
      'exe'   : 'application/octet-stream',
      'htm'   : 'text/html',
      'html'  : 'text/html',
      'json'  : 'application/json',
      'js'    : 'application/x-javascript',
      'txt'   : 'text/plain',
      'bmp'   : 'image/bmp',
      'rss'   : 'application/rss+xml',
      'atom'  : 'application/atom+xml',
      'gif'   : 'image/gif',
      'jpeg'  : 'image/jpeg',
      'jpg'   : 'image/jpeg',
      'jpe'   : 'image/jpeg',
      'png'   : 'image/png',
      'ico'   : 'image/vnd.microsoft.icon',
      'mpeg'  : 'video/mpeg',
      'mpg'   : 'video/mpeg',
      'mpe'   : 'video/mpeg',
      'qt'    : 'video/quicktime',
      'mov'   : 'video/quicktime',
      'wmv'   : 'video/x-ms-wmv',
      'mp2'   : 'audio/mpeg',
      'mp3'   : 'audio/mpeg',
      'rm'    : 'audio/x-pn-realaudio',
      'ram'   : 'audio/x-pn-realaudio',
      'rpm'   : 'audio/x-pn-realaudio-plugin',
      'ra'    : 'audio/x-realaudio',
      'wav'   : 'audio/x-wav',
      'zip'   : 'application/zip',
      'pdf'   : 'application/pdf',
      'xls'   : 'application/vnd.ms-excel',
      'ppt'   : 'application/vnd.ms-powerpoint',
      'wbxml' : 'application/vnd.wap.wbxml',
      'wmlc'  : 'application/vnd.wap.wmlc',
      'wmlsc' : 'application/vnd.wap.wmlscriptc',
      'spl'   : 'application/x-futuresplash',
      'gtar'  : 'application/x-gtar',
      'gzip'  : 'application/x-gzip',
      'swf'   : 'application/x-shockwave-flash',
      'tar'   : 'application/x-tar',
      'xhtml' : 'application/xhtml+xml',
      'snd'   : 'audio/basic',
      'midi'  : 'audio/midi',
      'mid'   : 'audio/midi',
      'm3u'   : 'audio/x-mpegurl',
      'tiff'  : 'image/tiff',
      'tif'   : 'image/tiff',
      'rtf'   : 'text/rtf',
      'wml'   : 'text/vnd.wap.wml',
      'wmls'  : 'text/vnd.wap.wmlscript',
      'xsl'   : 'text/xml',
      'xml'   : 'text/xml',
      'svg'   : 'image/svg+xml'
    };

http.createServer(function(request, response) {

    var uri = url.parse(request.url).pathname, filename = path.join(process.cwd(), uri);


    fs.exists(filename, function(exists) {
        if(!exists) {
            response.writeHead(404, {"Content-Type": "text/plain"});
            response.end("404 Not Found");
            return;
        }

        if (fs.statSync(filename).isDirectory()) filename += '/index.html';

        fs.readFile(filename, "binary", function(err, file) {

            if(err) {
                response.writeHead(500, {"Content-Type": "text/plain"});
                response.end(err);
                return;
            }

            var ext = filename.replace(/.*[\.\/\\]/, '').toLowerCase();

            response.writeHead(200, {"Content-Type": (mimeTypes[ext] || "text/plain")});
            response.end(file, "binary");
        });
    });

}).listen(parseInt(port, 10));

console.log("Static server running at  : http://localhost:" + port + "/\nCTRL + C to shutdown");

Inspired by: https://gist.github.com/respectTheCode/1926868

RedisLite - Key / Value storage implemented in Php

A simple storage class inspired by the redis api on top of Sqlite.

Usage:


    $db = new RedisLite("PATH_TO_WRITABLE_FOLDER/mydb");

    $db->set("mykey", 1);
    
    $db->incr("mykey");
    
    $db->get("mykey"); // 2

    // Lists

    db->rpush("mylist", "item1");
    db->rpush("mylist", "item2");
    $db->lpush("mylist", "item3");

    $db->get("mylist");  // ["item3", "item1", "item2"]

    // hashes

    $db->hset("myhash", "myfield", 1);
    $db->hmset("myhash", "myfield-1", "value-1", "myfield-2", "value-2");

    $db->hkeys("myhash"); // ["myfield", "myfield-1", "myfield-2"]
    $db->hvals("myhash"); // [1, "value-1", "value-2"]

    $db->hget("myhash", "myfield"); // 1

    $db->get("myhash"); // array("myfield"=>1, "myfield-1"=>"value-1", "myfield-2"=>"value-2")

Implemented methods:

set, get, exists, keys, del, type, incr, decr, 
llen, lpush, rpush, lset, lindex,
hset, hget, hgetall, hexists, hkeys, hvals, hlen, hincrby, hmset, hmget

MongoLite - NoSql implemented in Php

I needed a no-sql solution for small projects without the need of a full MongoDb server.

This is the result, a no-sql solution based on sqlite:


    $client     = new MongoLite\Client(PATH_TO_WRITABLE_FOLDER);
    $database   = $client->testdb;
    $collection = $database->products;

    $entry = ["name"=>"Super cool Product", "price"=>20];

    $collection->insert($entry);

    $products = $collection->find(); // Get Cursor

    if ($products->count()) {

        foreach($products->sort(["price"=>1])->limit(5) as $product) {
            var_dump($product);
        }
    }

More information + SourceCode: @Github