So I begun with node.js yesterday after watching some insane and cool apps on www.nodeknockout.com . The first thing to do was obviously to watch Ryan Dahl’s videos explaining the philosophy behind node.js. The major portion of it is about avoiding blocking I/O by using callback functions, together with an event loop based system. Not too novel, several similar frameworks already exist for this – think Twisted for python.
However, what it does do is allow JS developers earlier restricted to the front end/browser/desktop, to do I/O in an efficient way by integrating with Google’s V8 JS engine, and combining the power of POSIX libraries like libev (for select/poll/epoll) and libeio ( for threadpools which it uses to implement callbacks ).
So I began with node.js, and tried to implement an MTV platform ( the next thing to do after the Hello World tutorial is over ) a-la Django.
The source code for this project is at http://github.com/addictiv/nodjango
The entire thing comprises of 4 files :
- urls.js: You write the mappings from urls to view functions here, like you do in urls.py ( in Django).
- views.js : You define the view functions that you have mapped to ( in urls.js ) here, as you would do in views.py ( Django).
- controller.js : You don’t need to worry about this file as a user. This takes care of the HTTP request handling and URL dispatching, based on urls.js .
- runserver.js: You don’t need to worry about this file as a user. This takes care of starting the server and initializing it with a
request handler.
You run your webserver as :
> node runserver.js
Once you have run your webserver using the above command, go to your browser and request http://127.0.0.1:8000/url_path
If you have defined a mapping for ‘url_path’, in urls.js, the mapping’s view function in view.js is called and the appropriate content returned. If not, you get a 404 not found error.
As an example for what and how to write view functions, you can simply take a look at the existing view functions in views.js !
The code for this project is at http://github.com/addictiv/nodjango
So there you have it, your first trivial node.js app, up and running in less than an hour.
Next up, hopefully something nontrivial. :D