For about the past month and a half I’ve been playing with Node.js. Node.js is server-side JavaScript, using Google’s V8 JavaScript engine. Node.js can run a HTTP server, or any other type of socket connection. I’ve really only experimented with it’s HTTP server using the Express framework
I’m coming from a PHP background, so writing server backend’s in JavaScript while being asynchronous requires a slightly different mindset. In PHP, a user hits your Web server and Apache fork’s a new process, the PHP interpreter runs the script from top to bottom, then it gives the output to Apache and then dies. With Node.js, you have one process for the whole Web server. Also I find myself passing around the request and response objects a lot, unlike with PHP where you have a start.php
or config.php
file with variables accessible by any of the code. But after a while the stuff will come naturally to you.
I’ve started experimenting with writing a cloud based video encoding related application(not giving many details on that yet, not promising anything.) within Express, I use EJS(Embedded JavaScript) for template files so I can keep my application logic away from my views. I have configured my app I’m working on to have PHP styled open and close tags, since I’m used to template files being that way in PHP.
Another thing you have to do is define all your routes, guesses very similar to the frameworks like Ruby on Rails. Also with Express, I created a static file store to store my CSS, public JavaScript files, images and anything else I would want served statically.
Another thing I really love about Node.js is NPM(Node Package Manager). NPM is a package manager similar to apt-get that comes with Node.js. In the NPM, you can install modules, and publish modules. I’ve already wrote a few modules, to help me out on my own applications.
Some modules I wrote already are:
A module to check how much free space, and total space the hard drive has. Node.js didn’t really have any native functions for this. This module works on both Linux, Mac and Windows. On *nix systems it uses df -k
and on Windows it calls a small little C# application I wrote called DriveSpace(My first C# app) that comes bundled with the module.
A simple generic round-robin. Great for database connections pools.
A simple but powerful email wrapper around Nodemailer. Also uses Basic Template, another tiny module I wrote.
A module to detect if a IP is valid, it’s version and check if its within a certain range. I mainly wrote this to use in node_CloudFlare.
I use CloudFlare to supercharge my websites. I wanted this in Node.js, and I couldn’t really find any existing modules, So I wrote my own.
A simple module to mask emails, domains and even regular strings! Will turn [email protected]
into t**t@e****le.com
A simple yet powerful HTTP request library inspired by jQuery and LSL written in JavaScript for client and server JavaScript. I wanted a nice and clean simple to use HTTP request library for both the server and client, so I wrote one.
So those are some of the modules I wrote so far. I really enjoy Node.js, but sometimes it feels overkill compared to just whipping out a .php
and just writing something quickly. I guess I’ll get more used to Node.js and get better at it as time goes on. I also want to get in to mobile development, so I think Node.js would be a great tool to write backend’s in.