Node js Web application Development Part 1

Standard

fig,army,mens,ffffff

Nodejs  is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

This project will run on the node.js server with express and jade frameworks. The database is backed by mongodb :).

Installing nodejs.

This is really easy. Hit the Node.js website and click the big green Install button. It’ll detect your OS and give you the appropriate installer (if for some reason it doesn’t, click the downloads button and grab the one you need). Run the installer. That’s it, you have installed Node.js and, equally important, NPM – Node Package Manager – which lets you add all kinds of great stuff to Node quickly and easily.

  • Open a command prompt
  • cd to the directory in which you wish to keep your test apps
    (for the purposes of this example, C:\node).

Installing Express.

Express is a framework which is capable of turning the raw node into an application that behaves more like web servers.

To get express, type this command

C:\node>npm install -g express

This installs some core Express functionality right into our Node installation, making it available globally so we can use it anywhere we want. That’s handy. You’ll see a bunch of text in your command prompt, mostly a lot of http 304’s and GETs. That’s fine. Express is now installed and available.

Create an express project

We’re going to use Express and Jade, but not the Stylus CSS preprocessor (which people often use in this stack). We’re just going to use straight CSS for right now. We have to use Jade or another templating engine to gain access to our Node/Express-based data. Jade’s not hard to learn if you already know HTML. Just remember that you really have to pay attention to indentation or things will go badly wrong.

Anyway, still in c:\node or wherever you’re storing your node apps, type this:

C:\node>express –sessions nodetest1

This will generate something like this.

create : nodetest1
create : nodetest1/package.json
create : nodetest1/app.js
create : nodetest1/routes
create : nodetest1/routes/index.js
create : nodetest1/routes/user.js
create : nodetest1/views
create : nodetest1/views/layout.jade
create : nodetest1/views/index.jade
create : nodetest1/public/images
create : nodetest1/public/javascripts
create : nodetest1/public
create : nodetest1/public/stylesheets
create : nodetest1/public/stylesheets/style.css

install dependencies:
$ cd nodetest1 && npm install

run the app:
$ node app

Edit dependencies

open package.json according to this.


{
 "name": "application-name",
 "version": "0.0.1",
 "private": true,
 "scripts": {
 "start": "node app.js"
 },
 "dependencies": {
 "express": "3.4.7",
 "jade": "*",
 "mongodb": "*",
 "monk": "*"
 }
}

now do this command.

C:\node\nodetest1>npm install

Now run the application using following command.

C:\node\nodetest1>node app.js

Awesome! Open a browser and head for http://localhost:3000 where you will see a welcome to Express page.

browsershot1

Here is the sample Project on github.

https://github.com/supun/NodeExpress

One thought on “Node js Web application Development Part 1

Leave a comment