Posted on by & filed under Content - Highlights and Reviews, Programming & Development, Web Development.

Node.js is a software stack for writing server-side applications in JavaScript. It employs an event-driven, non-blocking I/O model to achieve high throughput rates with minimal resource consumption. As such, it is very well suited to writing scalable, real-time applications that are I/O bound.

Any software stack is only as useful as the number of useful, third party tools and frameworks available for it to make the lives of developers easier. Node.js is no exception, and it offers a plethora of options. One such framework which stands out from the crowd is the Express framework, which is a minimalist framework for writing server code.

To install express, type:

This will install the Express framework as a global package. To check the installation, type:

If the output is a list of commands and their explanation, the installation is complete. To create a new application, type:

<dir-name> is the name of the directory where you want to create the application. This directory can be empty, since it will be automatically created by the express command line tool. Once the command has finished executing, type:

This will change the working directory to <dir-name>, and install all dependencies specified in package.json (automatically created by the express command). Next type:

This will start a server at port 3000. Fire up a browser and head over to http://localhost:3000 and you will be greeted by a default page. This is because we haven’t added our own code yet, so open up your favorite text editor and open the file ‘app.js’. Delete all of the contents of the file and type the following:

The ‘express’ module exports a function that is called, and the contents returned are assigned to the app variable. On this app variable multiple functions of the form app.VERB are available where VERB is any one of the HTTP methods such as get, post, put, delete and so on. These functions can be used to easily create a RESTful API or even a complete web application. The functions accept a second argument which is a callback that accepts two parameters: request and response. These are essentially the same objects passed to Node’s built-in net.Server#listen() method, but have been augmented with more methods such as the send method that is used in the above example to directly return ‘Hello World!’ as a response. Read a robust introduction to Express in O’Reilly Media’s Learning Node, by Shelley Powers.

Type in ‘node app’ in your console and then navigate your browser to http://localhost:3000/. The page should display ‘Hello World!’.

Express adds many features on top of the default http server of Node.js, and of course covering all of that is well beyond the scope of this blog post. One feature that is of special interest; however, is middleware. Express supports defining special functions that can handle requests, called middleware, that can then pass execution to subsequent middleware and so on. Using this powerful concept, it is possible to handle complex communication scenarios, using a very elegant syntax. To define middleware (assuming the previous code snippet is in place):

Start the server by typing:

And then refresh your browser. You will see that with each refresh, the console will output the ‘Request received’ message (along with the requested URL). This powerful mechanism can be used from things like logging to static file serving. For an in-depth recipe that covers Views in the Express framework, read Templating in Express in Node Cookbook by David Mark Clements.

Safari Books Online has the content you need

Check out these Node.js books available from Safari Books Online:

This book shows you how to transfer your JavaScript skills to server side programming. With simple examples and supporting code, Node Cookbook talks you through various server side scenarios often saving you time, effort, and trouble by demonstrating best practices and showing you how to avoid security faux pas.
Take your web development skills from browser to server with Node—and learn how to write fast, highly scalable network applications on this JavaScript-based platform. With Learning Node, you’ll quickly master Node’s core fundamentals, gain experience with several built-in and contributed modules, and learn the differences and parallels between client- and server-side programming.
Node Web Development gives you an excellent starting point straight into the heart of developing server side web applications with node. You will learn, through practical examples, how to use the HTTP Server and Client objects, the Connect and Express application frameworks, the algorithms for asynchronous execution, and use both SQL and MongoDB databases.

About the author

Shaneeb Kamran is a Computer Engineer from one of the leading universities of Pakistan. His programming journey started at the age of 12 and ever since he has dabbled himself in every new and shiny software technology he could get his hands on. He is currently involved in a startup that is working on cloud computing products.

Tags: Express Framework, I/O Bound, middleware, Node.js, real-time, templating, views,

Comments are closed.