Express is a simple web application framework for building hybrid web applications. You can render web pages, generate customized responses and create user-friendly APIs quickly with ease. It just fits the trade with Node.js.
This post covers the installation and hello world example:
Installation:
buntu@ubuntu:~$ sudo npm install express
express@3.1.0 node_modules/express
├── methods@0.0.1
├── fresh@0.1.0
├── cookie-signature@0.0.1
├── range-parser@0.0.4
├── buffer-crc32@0.1.1
├── cookie@0.0.5
├── debug@0.7.2
├── commander@0.6.1
├── mkdirp@0.3.3
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.2 (pause@0.0.1, bytes@0.1.0, formidable@1.0.11, qs@0.5.1)
Hello World Example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var appln = express(); | |
appln.get('/', function(request, response){ | |
response.set('Content-Type', 'text/plain'); | |
response.send('hello world'); | |
}); | |
appln.listen(3000); | |
appln.set('Hello', 'Express Node'); |
In the above example:
- 1. express module is imported and application object is created
- 2. Object’s get() method is called and callback is set to respond with ‘hello world’
- 3. When user sends a GET request at port 3000, an event is generated that is handled by callback function(request, response)