Express: A Web Application Framework for Node.js

0
6658

Web element visual illustaration

Express is a Node.js web application framework designed for building single-page, multi-page and hybrid web applications. Its design is minimalistic but it has many features available as plugins and enables rapid development of Node.js based Web applications.

Express is a minimal Node.js Web application framework and is based on the core HTTP module. Last month, readers were introduced to Node.js. We are now ready to develop a Web application with the help of Express. Actually, Express is the backend of the MEAN stack (which comprises MongoDB, Express.js, Angular.js and Node.js).
Express is available on NPM and you can install it by running the command below:

$ npm install express --save

If you want to install Express temporarily, then there is no need to use the –save option. Express.js provides everything that the Connect component provides.

var connect = require(‘connect’)

A simple example of Express app code is shown below:

var express = require(‘express’);
var http = require(‘http’);
var port = 3000;

// create an express application object
var app = express();
app.use(function(req, res, next) {
res.end(‘Hello Express’);
});

// register with http
http.createServer(app).listen(port);

Actually, in Line 1 from the above code, we get a function that we can call to create an Express application. This application object app has the behaviour of the Connect dispatcher.

Serving static pages
The NPM module serve-static is designed for serving static pages. Just create an HTML file, save it as index.html and run the code below for you to get a simple Web server serving the page from the specified directory.

var express = require(‘express’);
var stPages = require(‘serve-static’);

var port = 3000;
var dir = ‘/public’;

var app = express();
app.use(stPages(__dirname + dir));
app.listen(port);

The Express response object and its methods
The Express response object is derived from the standard Node.js server response object. It has some utility functions too. Some frequently used functions are listed below:
1. append()
2. cookie()
3. clearCookie()
4. end()
5. get()
6. json()
7. redirect()
8. render()
9. set()
10. status()

The Express request object and its methods
This object is a wrapper for Node.js’ http.request object. The code, req.get(‘Content-Type’) ; returns the HTTP request header. The code, req.is(‘text/html’); returns ‘true’ if the MIME type is text/HTML.
Some frequently used functions are listed below:
1. accepts()
2. acceptsCharsets()
3. acceptsEncodings()
4. acceptsLanguages()
5. param()

Express routes
Express provides good URL based routing. The Express route object has the get, post, put and delete functions. Routing defines how to respond to a client request for a particular path (URI). We can have multiple handler functions for routes and they will be executed if the route is matched.

var express = require(‘express’);
var port = 3000;
var app = express();

app.get(‘/’, function (req, res) {
res.send(‘Hello World!’);
});

app.get(‘/user’, function (req, res) {
res.send(‘Requesting /user’);
});

app.get(‘/user/task’, function (req, res) {
res.send(‘Requesting /user/task’);
});
var server = app.listen(port, function () {
var host = server.address().hostName;
var port = server.address().port;
console.log(‘Open web browser and type http://localhost:%s in address bar!’, port);
});

Router
A Router object is an instance of routes. It can be created easily, as shown below, and we can also add the methods get, post, put and delete, just like we add them in applications.

var router = express.Router([options]);

Methods available in Router are:
1. all()
2. get()
3. post()
4. put()
5. delete()
6. param()
7. route()
8. use()

Some code snippets using Router are given here:

router.use(express.static(__dirname + ‘/public’));

router.use(function(req, res){
res.send(‘Hello World’);
});

Readers are advised to further explore the examples provided at http://expressjs.com/en/ .

References
[1] http://expressjs.com
[2] https://nodejs.org

LEAVE A REPLY

Please enter your comment!
Please enter your name here