Thursday, January 29, 2015

Simple Pure Node Server

I bet everyone who tried node, has written that famous sample server code which appears in nodejs.org homepage. Then without a blink most of us moved to express or some other node framework, with or without knowing what's happening under the hood. Well, for educational purposes I wrote a simple node server without using any npm module. For anyone who is interested in what's happening behind the scene, this would be a great starting point.

Following are the core modules used. "http" module for server creation, "fs" module for read static content from the disk and "path" module for work with file paths.

var http = require("http"),
    fs = require("fs"),
    path = require("path");

Static files are filtered using simple switch statement.

switch(extName) {
    case ".html": {
        contentType = "text/html";
        dirPath = "/public/views/";
    } break;
    case ".css": {
        contentType = "text/css";
        dirPath = "/public/css/";
    } break;      
    case ".js": {
        contentType = "text/javascript";
        dirPath = "/public/js/";
    } break;
}    
If requested file is available on the disk, read and send the response. If not redirect to the error page.

fs.exists(filePath, function(isExists) {
    if(isExists) {
        readAndSendFile(res, filePath, contentType,200);
    }
    else {
        redirect404();
    }
});    

Please find the complete code at github. Please note that there are many ways to improve this and I would recommend to checkout Danial Khosravi's blog on the same topic for improved version.

Friday, January 23, 2015

Useful Git Commands

When I'm working with git, once in a while I found myself searching through google for proper command syntax. For those who struggle like me and for myself, I decided to maintain a command list.

If you are using git for the first time you need to setup git with your email and name.

git config --global user.email "you@example.com"
git config --global user.name "john doe"

Create a new git repository in current directory

git init

Add created repository in github as "origin" using remote repository url.

git remote add origin https://github.com/someuser/anyrepo.git

Check current status of the repository.

git status

Add modified files to commit.

git add .
git add file.ext

Commit all modified files or commit file by file with a proper commit message.

git commit -m "commit message."
git commit file.ext -m "commit message."

Push committed files to the origin from master. usually goes like push to <remote> from <master>

git push origin master

Undo your last pushed commit

git push -f origin HEAD^:master

If you need to ignore certain files or folders you need to create a .gitignore file and the folder names as bellow.

#ignore all files in node_modules.
node_modules/

#ignore .ext files.
*.ext

#but track this file, even though all .ext files are ignored
!importantFile.ext

Thursday, January 15, 2015

Sorting Algorithms with Sound

Found this amazing video on youtube which shows sorting algorithms with sound. take a look.