BlogLatest news

NodeJS in 3,2,1…

27 March, 2012

nodejs-3steps

Entering the Node.js world brings lots of satisfaction for the people in web development. One of the benefits is that you don’t have to learn a new language, because you can use the same javascript on the browser to create server-side applications easily. However, it’s common to keep what we’ve learned regarding “old-school” javascript and not take advantage of the opportunities and flexibility that Node gives when structuring our applications.

That’s why a little push from one of the pioneers of this technology, TJ Holowaychuck (creator of the ExpressJS framework) comes in handy.

As I promised on the article’s title, to start using node.js with the right foot and run succesfully an application, you must follow the following 3 steps:

  1. Download node.js: http://nodejs.org/#download
  2. Install ExpressJS globally, using the package manager npm (which comes with node): In the terminal, write:
    npm install -g express
  3. Position yourself on the PARENT folder of our future project, and type:
    express projectname && cd projectname && npm install

This last step, creates a new project in node.js using the ExpressJS framework, and then it installs all the necessary dependencies (npm install). That’s it. If you want to view our sandbox, type on the terminal (inside the project folder) node app.js. Finally go to http://localhost:3000/ in your browser.

The Express installation, gives us the following:
  • app.js: Is the file that contains the server configuration and gathers the rest of the files that compose our app.
  • package.json: Has our project metadata. It’s very useful for distribution and for package managemente.
  • /public: The folder where we keep the static files.
  • /routes: The folder that contains the path archives and probably the app logic.
  • /views: The folder that contains the “views”. By default, Express uses Jade as a templating system.

On future posts, we’ll give you tips and recepies to work with node.js. This starting point, I hope, gives you the motivation to lose the fear of node and start creating powerful javascript applications. I also recommend visiting node’s official community where you’ll find excellent resources: http://nodejs.org/community/