Express - Node.js Web Framework
Jul 12, 2012
Suresh Vasudev
3 minute read

There are many Web Frameworks available which are built on various languages

  • ROR and Sinatra - Ruby
  • Zend and CodeIgniter - PHP

Express was inspired by Sinatra - A Ruby Framework.

Installing Express

Installing Express is a straightforward process using npm npm install -g express

-g option is used as we need to install this at system level and can be accessed from any projects within the system. Express being a Web Framework, it will be shared by multiple projects.

Creating the first Express application

To create an Express application execute the following command express {ApplicationName}

It will create a directory {ApplicationName} and will generate all the framework related files for the application.

EX

It also creates a package.json file and also reminds to install all the dependent modules. So let us follow these instructions and deploy all the modules.

EX

It also creates a package.json file and also reminds to install all the dependent modules. So let us follow these instructions and deploy all the modules.

EX

To confirm that the basic application setup is perfect let us start the app and check the default page

To start the app execute the command “node app.js” from the application directory. On successful start up it will display the following message “Express server listening on port 3000 in development mode”. To confirm it open a browser window and access the URL - http://localhost:3000. We could see a “Welcome” page in the browser window as response.

EX

Files and directories created by Express

Now we know that the application is working fine and so let us examine the files generated by Express. The major files and directories are app.js : The application file used to start the application. It contains the configuration information of the application.

node_modules: The directory where the dependent modules are installed as a part of “npm install” operation.

package.json: This file contains the dependent module information which will be used by “npm install” operation.

public: The folder to store images, javascripts and stylesheets. We should maintain any application logic in the files of these folders.

routes: This directory holds the routing information. The routes defines the pages that application should respond to.

views: Defines the layout of the application.

Please note that the directory structure created by Express is optional and we have the control to change it in the way we want.

Contents of a base express application


Directory of D:\SURESH\techthali\node\expressEg01
07/12/2012 12:44 AM <DIR> .
07/12/2012 12:44 AM <DIR> ..
07/12/2012 12:27 AM 836 app.js
07/12/2012 12:32 AM <DIR> node_modules
07/12/2012 12:27 AM 162 package.json
07/12/2012 12:27 AM <DIR> public
07/12/2012 12:27 AM <DIR> routes
07/12/2012 12:27 AM <DIR> views
3 File(s) 998 bytes
Directory of D:\SURESH\techthali\node\expressEg01\node_modules
07/12/2012 12:32 AM <DIR> .
07/12/2012 12:32 AM <DIR> ..
07/12/2012 12:32 AM <DIR> .bin
07/12/2012 12:31 AM <DIR> express
07/12/2012 12:32 AM <DIR> jade
0 File(s) 0 bytes
Directory of D:\SURESH\techthali\node\expressEg01\public
07/12/2012 12:27 AM <DIR> .
07/12/2012 12:27 AM <DIR> ..
07/12/2012 12:27 AM <DIR> images
07/12/2012 12:27 AM <DIR> javascripts
07/12/2012 12:27 AM <DIR> stylesheets
0 File(s) 0 bytes

Directory of D:\SURESH\techthali\node\expressEg01\routes
07/12/2012 12:27 AM <DIR> .
07/12/2012 12:27 AM <DIR> ..
07/12/2012 12:27 AM 116 index.js
1 File(s) 116 bytes
Directory of D:\SURESH\techthali\node\expressEg01\views
07/12/2012 12:27 AM <DIR> .
07/12/2012 12:27 AM <DIR> ..
07/12/2012 12:27 AM 32 index.jade
07/12/2012 12:27 AM 109 layout.jade
2 File(s) 141 bytes

Express uses template engine - Jade to generate HTML views. Jade is an indentation based template engine which took its inspiration from HAML (HTML Abstraction Markup Layer)