Omarrr

node.js, the morning after

Follow up post to Getting started with node.js covering the basic node app setup (package.json), npm package manager and the connect and express frameworks.

node.js

package.json

This file, located on the root of every node project, describes the basic configuration of your app. Mainly in contains metadata describing the app itself. It also details the package dependencies needed for the project.

{
    "name": "myApp",
    "author": "omarrr",
    "description": "my first node app",
    "version": "0.0.1",
    "dependencies": {
        "connect": "2.3.6",
        "express": "3.0.0beta5"
        },
    "engine": "0.8.1"
}

where engine” describes the version of node this app is running under.

npm

npm (node package manager) comes pre-bundled with node, it installs packages and manages dependencies

npm is to node what rvm is to Ruby.

npm install [package]

Install the dependencies in the local node_modules folder. npm defaults to always install all packages locally to your current project. You can overwrite this behavior with –global (but it’s probably a good idea not to :)

Basic usage

npm install

Will read the package.json file in your app dir and install all its dependencies

npm install package

Will install the package (registered with npm) in your local folder

Connect

Selfdescribed as a middleware framework for node” (or rather node’s HTTP server), Connect it’s build to easily plugin other middleware and comes itself bundled with a selection of them.

Connect is to node what Rack is to Ruby.

You can install connect by simply running:

$ npm install connect

This will create –if needed– a subfolder in your app folder called node_modules” where connect can be found (and it’s dependencies under another node_modules”).

Express

Express is a fast, unopinionated, minimalist web framework for node”. Express sits on top of Connect to extend it, as Connect sits on top of node’s http.

Express is to node what Sinatra is to Ruby.

You can install express by simply running:

$ npm install express

This will also store the package and its dependencies under the node_modules” folder.

Creating a fresh Express app

Express can also easyly generate an app skelleton for new express+node projects.

To do this, express must be installed globally:

$ npm install -g express

Once express is globally available it can create the app

$ express myApp

This will create a new folder named myApp” and create a new node server app that utilizes the Express framework.

Next we need to install all the new project dependencies (listed on the newly created ./myApp/package.json)

$ cd myApp
$ npm install

Now we can run the server

$ node app.js

July 4, 2012 ☼ codeconnectexpressjsnodenode jsnpmpackage json