News
A nodejs app from ground up – part 1
On 09, Jan 2012 | No Comments | In News | By csrui
I’ve been a developer for many years and I’ve been fortunate enough to have built projects in several different languages, but lately I’ve been working mostly with PHP and Javascript and wondering about the PHP part.
But what I’ve noticed is that I’ve been spending more and more time with Javascript and less time with PHP. Although I love good old PHP (thank’s to CakePHP) I find myself using it only to build API’s to consume or send data to a database. This fact is easily justified with the current shift from desktop to mobile consumption and the constant rise of Javascript’s popularity.
Corporate adoption of Javascript and easy to use libraries make it a language worth mastering specially since these days it’s possible to build a single application using HTML and Javascript and run it on web servers, mobile devices and desktop environments.
This led me to a very important question, why should I keep working with two different languages when I can do everything with just one. This simple question made me spend a hole lot of hours reading up on the possibilities of NodeJs and it turns out that I can just scratch PHP (don’t be like that, it’s not you, it’s me).
I believe that there may be other developers out there questioning them selfs so, for mine and their sake I decided to spend my saturday putting to practice what I’ve learned and just see how it feels.
Since I use CakePHP or SLIM frameworks I’m used to having some fancy stuff on my projects like:
- Routing
- Templating
- I18n
- Database abstraction
I’ll try to be as descriptive of the process as I can be and try to link up with every resource I used.
So, put on your helmets and here we go.
First of all, we need to install Node and you can do so by heading of to http://nodejs.org and clicking on download. I’ve downloaded the Macintosh Installer.
As you can see, Node was installed as was NPM. This allows me to use NPM to install modules to use in my apps. More on NPM after the commercials.
Ok, now that we have Node installed, what’s next? Here’s a thought… why should we code everything, in every app. Let’s start by using some sort of framework to speed up development. From what I’ve seen a good choice is Express because the documentation is fenomenal and since we are starting out, we shouldn’t need to spend extra hours figuring out how the framework well… works.
Remember NPM? we will use it now to install Express.
Here’s how:
$ npm install -g express $ npm install -d
The first command globally installs Express, an app framework featuring Routing, View rendering and Sessions and the second installs all the dependencies.
To create a new application just create a new folder and type
$ express
This should have created a bunch of files and folders containing your Views, Routes and Public files. Before we start breaking stuff, let’s fire up the server and see if it works.
$ node app.js
If you open your browser and point it to http://127.0.0.1:3000/ you should see something like the screenshot below.
If you do, you now have a starting point for completely changing the way you work.
Stay tunned for part 2 of this post.








Submit a Comment