
Empowering Express with Node modules
Express does not come packed with a huge bunch of built-in libraries to perform tasks that are beyond a basic website. Express is very minimal. But that does not mean that it is not capable of performing complex tasks.
You have a huge collection of Node modules on the npm registry that can be easily plugged in to your app and used for performing all sorts of tasks in the app.
In Chapter 1, What is Express?, we were introduced to Node modules, and we learned how to write them. We also found out that they can be used to modularly extend the power and capability of Express.
You could write your own Node modules to accomplish many things, but anything you are trying to achieve, probably there is an excellent open source Node module out there already. You just need to find the right module, install it, and use it in your app.
Note
The npm registry/network in a publicly available online resource where Node developers publish their Node modules. These modules are installed using the npm
command.
You can find a huge list of Node modules at https://github.com/joyent/node/wiki/Modules. From the command line, you can use the npm search
command or use a module such as npm-search
or npm-research
to search for modules of your interest.
Let's find out how we can install and use a Node module from the NPM registry. We will install a .ini
file parsing module named iniparser
and use it in our app:
$ npm install iniparser npm WARN package.json application-name@0.0.1 No README.md file found! npm http GET https://registry.npmjs.org/iniparser npm http 304 https://registry.npmjs.org/iniparser iniparser@1.0.5 node_modules/iniparser
The module has been installed successfully. Create config.ini
in the app directory with the following content:
title = My Awesome App port = 3000 message = You are awesome!
You might have guessed it already, we are going to use this as the configuration file for our app.
Now edit app.js
to include the module and use it in our app:
var http = require('http'); var express = require('express'); var app = express(); // Load the iniparser module var iniparser = require('iniparser'); // Read the ini file and populate the content on the config object var config = iniparser.parseSync('./config.ini'); app.set('view engine', 'jade'); app.set('views', './views'); app.use(express.static('./public')); app.use(express.responseTime()); app.use(app.router); app.use(express.errorHandler()); app.get('/', function(req, res) { // Pass two config variables to the view res.render('index', {title:config.title, message:config.message}); }); http.createServer(app).listen(config.port, function() { console.log('App started on port ' + config.port); });
While we are at it, we'd like to increase the complexity of the view a little bit more, so go ahead and edit index.jade
too:
html head title #{title} script(src='javascripts/main.js') link(rel='stylesheet', href='stylesheets/style.css') body #content img(src='images/logo.png') p WELCOME p #{message} #smile
Restart the app and load it in the browser to see the "drastic" changes:

So, you see Express does not come with the inherent ability to parse .ini
files, but has the extensibility to be able to do almost anything with the correct Node module, because of which we can parse .ini
files and use one in our app.