Chapter 7. Important External Modules
Although the Node core is extremely useful, many of its abstractions are very low-level. So a lot of development in Node is done using higher abstraction libraries built by the community, similar to how Ruby-based websites use Rails or Sinatra rather than custom-written Ruby code. Although these modules aren’t technically Node itself, they are extremely important for getting things done, and many of them are mature projects in themselves. This chapter explores some of the most popular and useful community modules for Node.
Express
Express, an MVC framework for Node, is probably the most widely used Node module. It was inspired by the Sinatra framework for Ruby and enables a lot of features that make it very easy to throw together a website with Node.
A Basic Express App
Express works by defining page handlers for
routes. The routes can be as simple as a path, or
much more complex. The handlers could be as simple as emitting “Hello,
world” or as complex as a whole page-rendering system that interacts
with a database. You’ll need to install Express using npm install express before you can start using it. Example 7-1
shows how to create a simple application with Express.
Example 7-1. Creating a simple Express app
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res) {
res.send('hello world');
});
app.listen(9001);This code is obviously pretty similar to http in terms of creating a server. However, a few things ...